CMake 生成的 MSVC 项目找不到符号,即使为其相关 dll 正确生成了 lib 文件也是如此
CMake generated MSVC project cannot find symbols even if lib files are correctly generated for their correlated dlls
在过去的几天里,我一直在抨击这个问题(我的回购在下面链接(。我希望 CMake 生成一个 MSVC 解决方案,其中引擎项目链接到演示项目。虽然 .lib 和 .dll 文件已正确生成并添加到 MSVC 中的项目中,但我仍然从演示项目中收到未解决的链接器符号错误,这些链接器符号引用了引擎项目中的符号。Engine.lib 文件被正确添加到 demo 的依赖项中,标头也是如此。我没有需要使用生成的导出标头的全局变量。这里有什么问题?
Root CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(P_SentryAll)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
include(GenerateExportHeader)
# glob source and header files
file(GLOB_RECURSE EngineSources SENTRY.Engine/*.cpp SENTRY.Engine/*.hpp)
file(GLOB_RECURSE CoreSources SENTRY.Core/*.cpp SENTRY.Core/*.hpp)
file(GLOB_RECURSE RenderSources SENTRY.Render/*.cpp SENTRY.Render/*.hpp)
file(GLOB_RECURSE DemoSources SENTRY.Demo/*.cpp SENTRY.Demo/*.hpp)
file(GLOB_RECURSE EngineHeaders SENTRY.Engine/*.hpp)
file(GLOB_RECURSE CoreHeaders SENTRY.Core/*.hpp)
add_subdirectory(SENTRY.Core)
add_subdirectory(SENTRY.Engine)
add_subdirectory(SENTRY.Render)
add_subdirectory(SENTRY.Demo)
Root/Sentry.Core/CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
# define project
project(P_SentryCore)
add_library(SentryCore SHARED ${CoreSources})
target_include_directories(SentryCore PUBLIC src/module)
generate_export_header(SentryCore)
install(TARGETS SentryCore DESTINATION lib)
install(FILES ${CoreHeaders} DESTINATION include/SentryCore)
Root/Sentry.Engine/CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(P_SentryEngine)
add_library(SentryEngine SHARED ${EngineSources})
target_link_libraries(SentryEngine PUBLIC SentryCore)
target_include_directories(SentryEngine PUBLIC src/engine)
generate_export_header(SentryEngine)
install(TARGETS SentryEngine DESTINATION lib)
install(FILES ${EngineHeaders} DESTINATION include/SentryEngine)
Root/Sentry.Demo/CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
# define project
project(P_SentryDemo)
add_executable(SentryDemo ${DemoSources})
target_link_libraries(SentryDemo PUBLIC SentryEngine)
include_directories(SENTRY.Engine/src/engine SENTRY.Core/src/module)
# packaging
install(TARGETS SentryDemo DESTINATION build)
错误:
1>------ Build started: Project: ZERO_CHECK, Configuration: Debug x64 ------
2>------ Build started: Project: SentryEngine, Configuration: Debug x64 ------
2>Engine.cpp
2>Auto build dll exports
2> Creating library C:/Users/main/Desktop/Projects/SENTRY/Build/SENTRY.Engine/Debug/SentryEngine.lib and object C:/Users/main/Desktop/Projects/SENTRY/Build/SENTRY.Engine/Debug/SentryEngine.exp
2>SentryEngine.vcxproj -> C:UsersmainDesktopProjectsSENTRYBuildSENTRY.EngineDebugSentryEngine.dll
3>------ Build started: Project: SentryDemo, Configuration: Debug x64 ------
3>SENTRY.Demo.obj : error LNK2019: unresolved external symbol "public: void __cdecl Engine<__int64,struct std::ratio<1,1000000> >::Init(void)" (?Init@?$Engine@_JU?$ratio@$00$0PECEA@@std@@@@QEAAXXZ) referenced in function main
3>SENTRY.Demo.obj : error LNK2019: unresolved external symbol "public: void __cdecl Engine<__int64,struct std::ratio<1,1000000> >::Run(void)" (?Run@?$Engine@_JU?$ratio@$00$0PECEA@@std@@@@QEAAXXZ) referenced in function main
3>SENTRY.Demo.obj : error LNK2019: unresolved external symbol "public: void __cdecl Engine<__int64,struct std::ratio<1,1000000> >::RegisterModule(class Module<__int64,struct std::ratio<1,1000000> > *)" (?RegisterModule@?$Engine@_JU?$ratio@$00$0PECEA@@std@@@@QEAAXPEAV?$Module@_JU?$ratio@$00$0PECEA@@std@@@@@Z) referenced in function main
3>C:UsersmainDesktopProjectsSENTRYBuildSENTRY.DemoDebugSentryDemo.exe : fatal error LNK1120: 3 unresolved externals
3>Done building project "SentryDemo.vcxproj" -- FAILED.
4>------ Skipped Build: Project: INSTALL, Configuration: Debug x64 ------
4>Project not selected to build for this solution configuration
========== Build: 2 succeeded, 1 failed, 2 up-to-date, 1 skipped ==========
回购
Root/Sentry.Demo/CMakeLists.txt
中的行:
include_directories(SENTRY.Engine/src/engine SENTRY.Core/src/module)
似乎不正确。它使用相对路径,所以我认为这些是项目中的有效路径:
Root/Sentry.Demo/SENTRY.Engine/src/engine
Root/Sentry.Demo/SENTRY.Core/src/module
首选通过使用CMAKE_SOURCE_DIR
变量尽可能使用绝对路径。此变量提供顶级源目录的路径。因此,请尝试以下操作:
include_directories(
${CMAKE_SOURCE_DIR}/SENTRY.Engine/src/engine
${CMAKE_SOURCE_DIR}/SENTRY.Core/src/module
)
我又看了一下你的存储库,也许更重要的是,你必须在头文件中Engine
而不是源文件中具有模板函数的完整定义。
因此,将这些函数定义移动到Engine
类定义中的头文件中:
template<typename T_rep, typename T_ratio>
void Engine<T_rep, T_ratio>::Init()
{
for (auto& module_ : Modules)
{
module_->Init();
}
}
template<typename T_rep, typename T_ratio>
void Engine<T_rep, T_ratio>::Run()
{
RunUpdateLoop = true;
auto TPStart = std::chrono::steady_clock::now();
auto TPEnd = TPStart;
while (RunUpdateLoop)
{
auto deltaT = TPEnd - TPStart;
TPStart = std::chrono::steady_clock::now();
for (auto& module_ : Modules)
{
module_->Run((deltaT));
}
TPEnd = std::chrono::steady_clock::now();
}
}
template<typename T_rep, typename T_ratio>
void Engine<T_rep, T_ratio>::RegisterModule(Module<T_rep, T_ratio>* ToRegister)
{
Modules.push_back(ToRegister);
}
这应该有助于您走上正确的轨道。
相关文章:
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 解析引用 |exe/dll vs lib
- CMake 生成的 MSVC 项目找不到符号,即使为其相关 dll 正确生成了 lib 文件也是如此
- 从 .lib 文件创建一个.dll文件
- 可执行文件C++包括.obj,.lib和.dll吗?
- 无法在 DLL 中链接 SDL2-static.lib
- 链接器找不到导入的 DLL 的 LIB 文件
- C++:重命名dll和lib并链接
- C++ CLR dll 需要 .lib 文件,但非 CLR 不需要
- 如何找出依赖于LIB文件的DLL列表?
- 链接 .lib 并在 Windows 中使用 .dll 在 C++ 在 Eclipse 中
- 将标头和 lib 转换为 dll 以将其与 c# 一起使用
- 为什么我的程序寻找共享资源.dll而不是Commons.lib
- 从.dll中调用.lib in C
- GHCi 无法在 Windows 上加载.dll lib(C++ lib)
- 如果在 DLL/Lib 中定义了对象,是否可以在客户端中实例化对象?
- "/MD /MT"和"dll lib"之间的概念歧义
- 在dll/lib上创建链接
- 如何检查 dll/lib 中的函数是否在 windows 中"C"?