【问题标题】:CMake generated MSVC project cannot find symbols even if lib files are correctly generated for their correlated dlls即使为其相关的 dll 正确生成了 lib 文件,CMake 生成的 MSVC 项目也找不到符号
【发布时间】:2020-01-24 08:47:07
【问题描述】:

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

根 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:\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

【问题讨论】:

  • 用你的代码进行 repo 很好,但 Stack Overflow 仍然要求对应的 code在问题帖子中 本身。请解决这个问题。欲了解更多信息,请参阅How to Ask
  • 连同代码,请在您的问题帖子中添加确切错误消息。如果没有看到错误本身,很难推测问题可能是什么。

标签: c++ visual-studio visual-c++ cmake


【解决方案1】:

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
)

我再次查看了您的存储库,也许更重要的是,您必须在 header 文件中拥有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);
}

这应该可以帮助您走上正轨。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多