如何在一个 DLL 中定义运行时常量并在另一个 DLL 中使用它们?
How to define runtime constants in a DLL and use them in another DLL?
我将此示例创建为 3 个 Visual Studio 项目:一个名为 'Constants'
的 DLL 库包含运行时常量 (IDs
),另一个名为 'Functions'
的 DLL 库使用第一个库中的常量,以及通过调用 'Functions'
库中的函数打印出常量的控制台 EXE。
Constants/src/Constants.h:
#pragma once
__declspec(dllexport) extern const int ID1;
__declspec(dllexport) extern const int ID2;
namespace {
int nextID();
}
Constants/src/Constants.cpp:
#include "Constants.h"
const int ID1 = nextID();
const int ID2 = nextID();
namespace {
int nextID() {
static int ID = 0;
return ID++;
}
}
Functions/src/Functions.h:
#pragma once
__declspec(dllexport) int getID1();
__declspec(dllexport) int getID2();
Functions/src/Functions.cpp:
#include "Functions.h"
#include "Constants.h"
int getID1() {
return ID1;
}
int getID2() {
return ID2;
}
Console/src/Main.cpp:
#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;
}
如果我 link 'Constants'
库直接到 EXE 并直接打印常量,那么一切正常,但如果我 link 'Constants'
到 'Functions'
然后 'Functions'
到 EXE(使用 getID
函数打印)然后我得到这个错误:
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 ==========
我不明白为什么在这种情况下 link 从 DLL 到 DLL 失败。
Constants.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();
}
Functions.h
应该是:
#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();
然后FUNCTIONS_DLLEXPORT
需要添加到'Functions'
项目的'Preprocessor Definitions',CONSTANTS_DLLEXPORT
添加到'Constants'
的'Preprocessor Definitions' ] 项目。
我将此示例创建为 3 个 Visual Studio 项目:一个名为 'Constants'
的 DLL 库包含运行时常量 (IDs
),另一个名为 'Functions'
的 DLL 库使用第一个库中的常量,以及通过调用 'Functions'
库中的函数打印出常量的控制台 EXE。
Constants/src/Constants.h:
#pragma once
__declspec(dllexport) extern const int ID1;
__declspec(dllexport) extern const int ID2;
namespace {
int nextID();
}
Constants/src/Constants.cpp:
#include "Constants.h"
const int ID1 = nextID();
const int ID2 = nextID();
namespace {
int nextID() {
static int ID = 0;
return ID++;
}
}
Functions/src/Functions.h:
#pragma once
__declspec(dllexport) int getID1();
__declspec(dllexport) int getID2();
Functions/src/Functions.cpp:
#include "Functions.h"
#include "Constants.h"
int getID1() {
return ID1;
}
int getID2() {
return ID2;
}
Console/src/Main.cpp:
#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;
}
如果我 link 'Constants'
库直接到 EXE 并直接打印常量,那么一切正常,但如果我 link 'Constants'
到 'Functions'
然后 'Functions'
到 EXE(使用 getID
函数打印)然后我得到这个错误:
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 ==========
我不明白为什么在这种情况下 link 从 DLL 到 DLL 失败。
Constants.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();
}
Functions.h
应该是:
#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();
然后FUNCTIONS_DLLEXPORT
需要添加到'Functions'
项目的'Preprocessor Definitions',CONSTANTS_DLLEXPORT
添加到'Constants'
的'Preprocessor Definitions' ] 项目。