C++ 如何在一个DLL中定义运行时常量并在另一个DLL中使用它们?

C++ 如何在一个DLL中定义运行时常量并在另一个DLL中使用它们?,c++,dll,visual-studio-2017,linker-errors,C++,Dll,Visual Studio 2017,Linker Errors,我将此示例创建为3个Visual Studio项目:一个名为'Constants'的DLL库,其中包含运行时常量(id),另一个名为'Functions'的DLL库,使用第一个库中的常量,还有一个控制台EXE,它通过调用“函数”库中的函数来打印常量 常量/src/Constants.h: 常量/src/Constants.cpp: Functions/src/Functions.h: Functions/src/Functions.cpp: 控制台/src/Main.cpp: 我不明白为什么在

我将此示例创建为3个Visual Studio项目:一个名为
'Constants'
的DLL库,其中包含运行时常量(
id
),另一个名为
'Functions'
的DLL库,使用第一个库中的常量,还有一个控制台EXE,它通过调用
“函数”
库中的函数来打印常量

常量/src/Constants.h: 常量/src/Constants.cpp: Functions/src/Functions.h: Functions/src/Functions.cpp: 控制台/src/Main.cpp:
我不明白为什么在这种情况下从DLL链接到DLL会失败。

常量。h
应该是:

#pragma once

#ifdef CONSTANTS_DLLEXPORT
#define CONSTANTS_API __declspec(dllexport)
#else
#define CONSTANTS_API __declspec(dllimport)
#endif

CONSTANTS_API extern const int ID1;
CONSTANTS_API extern const int ID2;

namespace {

    int nextID();

}
#pragma once

#ifdef FUNCTIONS_DLLEXPORT
#define FUNCTIONS_API __declspec(dllexport)
#else
#define FUNCTIONS_API __declspec(dllimport)
#endif

FUNCTIONS_API int getID1();
FUNCTIONS_API int getID2();
函数.h
应为:

#pragma once

#ifdef CONSTANTS_DLLEXPORT
#define CONSTANTS_API __declspec(dllexport)
#else
#define CONSTANTS_API __declspec(dllimport)
#endif

CONSTANTS_API extern const int ID1;
CONSTANTS_API extern const int ID2;

namespace {

    int nextID();

}
#pragma once

#ifdef FUNCTIONS_DLLEXPORT
#define FUNCTIONS_API __declspec(dllexport)
#else
#define FUNCTIONS_API __declspec(dllimport)
#endif

FUNCTIONS_API int getID1();
FUNCTIONS_API int getID2();

然后需要将
函数\u DLLEXPORT
添加到
“函数”
项目的“预处理器定义”中,将
常量\u DLLEXPORT
添加到
“常量”
项目的“预处理器定义”中。

它需要是定义它的DLL中的DLLEXPORT,但是dllimport在任何使用它的代码中都会出现。这看起来完全是错误的。你说的“链接”是什么意思?如果你有两个独立的模块在运行,你不能从一个到另一个another@Ruks我做了一个完整的回答,并将问题回复到原来的问题。我会在这里避免使用DECLSPEC这个名称。所有外壳变体中的单词
declspec
应保留用于指定声明的操作。结果应该描述类似函数\u API的意图,类似于Windows API函数的WINAPI。@harper我根据您的建议更改了答案。
#include <iostream>
#include <Functions.h>

int main() {
    std::cout << "Library Constants: " << std::endl;
    std::cout << getID1() << std::endl;
    std::cout << getID2() << std::endl;
    std::cin.get();
    return 0;
}
1>------ Rebuild All started: Project: Constants, Configuration: Debug Win32 ------
1>Constants.cpp
1>   Creating library C:\dev\DLLTest\bin\Win32\Debug\constants.lib and object C:\dev\DLLTest\bin\Win32\Debug\constants.exp
1>Constants.vcxproj -> C:\dev\DLLTest\bin\Win32\Debug\constants.dll
2>------ Rebuild All started: Project: Functions, Configuration: Debug Win32 ------
2>Functions.cpp
2>   Creating library C:\dev\DLLTest\bin\Win32\Debug\functions.lib and object C:\dev\DLLTest\bin\Win32\Debug\functions.exp
2>Functions.obj : error LNK2001: unresolved external symbol "int const ID1" (?ID1@@3HB)
2>Functions.obj : error LNK2001: unresolved external symbol "int const ID2" (?ID2@@3HB)
2>C:\dev\DLLTest\bin\Win32\Debug\functions.dll : fatal error LNK1120: 2 unresolved externals
2>Done building project "Functions.vcxproj" -- FAILED.
3>------ Rebuild All started: Project: Console, Configuration: Debug Win32 ------
3>Main.cpp
3>Console.vcxproj -> C:\dev\DLLTest\bin\Win32\Debug\console.exe
========== Rebuild All: 2 succeeded, 1 failed, 0 skipped ==========
#pragma once

#ifdef CONSTANTS_DLLEXPORT
#define CONSTANTS_API __declspec(dllexport)
#else
#define CONSTANTS_API __declspec(dllimport)
#endif

CONSTANTS_API extern const int ID1;
CONSTANTS_API extern const int ID2;

namespace {

    int nextID();

}
#pragma once

#ifdef FUNCTIONS_DLLEXPORT
#define FUNCTIONS_API __declspec(dllexport)
#else
#define FUNCTIONS_API __declspec(dllimport)
#endif

FUNCTIONS_API int getID1();
FUNCTIONS_API int getID2();