gpt4 book ai didi

c++ - 即使为其相关的 dll 正确生成了 lib 文件,CMake 生成的 MSVC 项目也找不到符号

转载 作者:行者123 更新时间:2023-12-02 10:21:38 27 4
gpt4 key购买 nike

在过去的几天里,我一直在抨击这个问题(我的 repo 链接在下面)。我希望 CMake 生成一个 MSVC 解决方案,其中 Engine 项目链接到 Demo 项目。虽然 .lib 和 .dll 文件已正确生成并添加到 MSVC 中的项目中,但我仍然从 Demo 项目中从 Engine 项目中引用符号得到 Unresolved 链接器符号错误。 Engine.lib 文件已正确添加到 Demo 的依赖项中,头文件也是如此。我没有需要使用生成的导出 header 的全局变量。这里有什么问题?

根 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)

根/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)

根/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)

根/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:\Users\main\Desktop\Projects\SENTRY\Build\SENTRY.Engine\Debug\SentryEngine.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:\Users\main\Desktop\Projects\SENTRY\Build\SENTRY.Demo\Debug\SentryDemo.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 ==========

Repo

最佳答案

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
)

我又看了你的 repo ,也许更重要的是,你必须有 已满 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);
}

这应该有助于您走上正确的轨道。

关于c++ - 即使为其相关的 dll 正确生成了 lib 文件,CMake 生成的 MSVC 项目也找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59892994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com