提问人:Jack Benson 提问时间:12/20/2022 更新时间:12/20/2022 访问量:240
构建 C++ 静态库时,什么原因导致隐藏在源文件中的第三方库成为最终用户依赖项?
When building a C++ static library what causes third party libraries hidden in source files to become end user dependencies?
问:
对不起,如果这个问题有点啰嗦。我知道这里已经覆盖了很多领域。
本质上,当我将我的 tst_engine.lib 包含在另一个目标中时,我收到以下错误。
lld-link: error: undefined symbol: SDL_Init
>>> referenced by C:\Users\n10255460\CLionProjects\2D_engine_and_game\tst_engine\TstGameEngine.cpp:22
>>> tst_engine.lib(TstGameEngine.cpp.obj):(public: __cdecl tst::TstGameEngine::TstGameEngine(bool *))
lld-link: error: undefined symbol: SDL_Quit
>>> referenced by C:\Users\n10255460\CLionProjects\2D_engine_and_game\tst_engine\TstGameEngine.cpp:43
>>> tst_engine.lib(TstGameEngine.cpp.obj):(public: __cdecl tst::TstGameEngine::~TstGameEngine(void))
ninja: build stopped: subcommand failed.
如上所述,SDL 用于 .cpp 文件而不是 .h 文件。最重要的是,当我尝试将 SDL 作为依赖项包含在我尝试将 tst_engine 作为库进行编译的目标中时,此错误就会消失。
据我之前的理解,如果第三方库位于源文件中,则它永远不应该成为使用与源文件关联的库的人的依赖项。以下是两个相关的 cmake 文件:
触发错误的可执行文件:
cmake_minimum_required(VERSION 3.24)
project(game)
set(CMAKE_CXX_STANDARD 20)
include_directories(../tst_engine)
set(lib ${CMAKE_SOURCE_DIR}\\libraries\\lib)
include_directories(${CMAKE_SOURCE_DIR}\\libraries\\include)
add_executable(game main.cpp)
target_link_libraries(game
${CMAKE_SOURCE_DIR}\\cmake-build-debug\\tst_engine\\tst_engine.lib
)
编译成功的库:
cmake_minimum_required(VERSION 3.24)
project(tst_engine)
set(CMAKE_CXX_STANDARD 20)
set(lib ${CMAKE_SOURCE_DIR}\\libraries\\lib)
include_directories(${CMAKE_SOURCE_DIR}\\libraries\\include)
add_library(tst_engine STATIC TstGameEngine.h TstGameEngine.cpp)
target_link_libraries(tst_engine PRIVATE ${lib}\\SDL2main.lib ${lib}\\SDL2.lib ${lib}\\SDL2-static.lib)
答: 暂无答案
评论
PRIVATE
PUBLIC
target_link_libraries
target_compile_features(tst_engine PUBLIC cxx_std_20)
target_link_directories
target_include_directories
set()
include_directories