Я создал этот пример как 3 проекта Visual Studio: библиотека DLL с именем 'Constants'
, которая содержит константы времени выполнения (IDs
), другая библиотека DLL с именем 'Functions'
, которая использует константы из первой библиотеки, и консольный EXE-файлкоторый выводит константы, вызывая функции из библиотеки 'Functions'
.
![enter image description here](https://i.stack.imgur.com/zVWXd.png)
Constants / src / Constants.h:
#pragma once
__declspec(dllexport) extern const int ID1;
__declspec(dllexport) extern const int ID2;
namespace {
int nextID();
}
Константы / src / Constants.cpp:
#include "Constants.h"
const int ID1 = nextID();
const int ID2 = nextID();
namespace {
int nextID() {
static int ID = 0;
return ID++;
}
}
Функции / src / Functions.h:
#pragma once
__declspec(dllexport) int getID1();
__declspec(dllexport) int getID2();
Функции / 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;
}
Если я связываю библиотеку 'Constants'
напрямую с EXE и печатаю константы напрямую, то все работает нормально, но если я связываю '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 ==========
Я не понимаю, почему в этом случае не удается связать DLL с DLL.