gpt4 book ai didi

c++ - 找不到 CMake 静态库的依赖项

转载 作者:行者123 更新时间:2023-11-30 02:17:12 27 4
gpt4 key购买 nike

我正在构建一个静态库并将其链接到我的可执行目标中。该库在真空中构建良好,但当我尝试包含库中的 header 时,出现“没有这样的文件”错误,指向静态库的依赖项。

我的理解是我的静态库应该私下包含它的依赖项,并且消费者除了链接库之外不需要做任何事情。这是错误的吗?还是我只是错误地包含了静态库的依赖项?

静态库的 CMakeLists.txt:

# Find SDL2 and associated libs
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_mixer REQUIRED)
find_package(SDL2_ttf REQUIRED)

# Build static library dependency SDL2pp
set(SDL2PP_WITH_IMAGE ON)
set(SDL2PP_WITH_MIXER ON)
set(SDL2PP_WITH_TTF ON)
add_subdirectory("${PROJECT_SOURCE_DIR}/../Libraries/libSDL2pp/"
"${CMAKE_CURRENT_BINARY_DIR}/Libraries/libSDL2pp/")


# Add our static library target
add_library(Framework_Game STATIC
Private/GameManager.cpp
Public/GameManager.h
)

target_include_directories(Framework_Game
PRIVATE
${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
${SDL2PP_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/Private
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/Public
)

target_link_libraries(Framework_Game
PRIVATE
${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
${SDL2PP_LIBRARIES}
)

可执行文件的 CMakeLists.txt:

add_executable(NewWorlds Launch.cpp)

target_link_libraries(NewWorlds
PRIVATE
Framework_Game
)

GameManager.h 包含带有 #include <SDL2pp/SDL2pp.hh 的 SDL2pp .然后我将它包含在带有 #include <GameManager.h> 的 Launch.cpp 中,这给出了错误 SDL2pp/SDL2pp.hh: No such file or directory

最佳答案

您将 Framework_Game 的 SDL2pp 依赖项设为私有(private),因此它不会传播到依赖于 Framework_Game 的目标。我不知道 SDL2pp 库,所以我不能确切地说出哪些需要公开,但是

target_include_directories(Framework_Game
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/Private
PUBLIC
${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
${SDL2PP_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/Public
)

target_link_libraries(Framework_Game
PUBLIC
${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
${SDL2PP_LIBRARIES}
)

绝对有效。

关于c++ - 找不到 CMake 静态库的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53998330/

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