我有一个这样的项目目录设置:
3rdparty
|--glfw
test
|--3rdparty
|--googletest
|--CMakeLists.txt (Test project CMakeLists)
CMakeLists.txt (Main project CMakeLists)
在主项目中设置外部依赖项:
add_subdirectory(3rdparty/glfw)
target_link_libraries(Main glfw)
对Test项目也应用所有依赖项的最佳方法是什么?我是否应该在子CMakeLists.txt中重做所有依赖项代码?预先感谢
答案 0 :(得分:1)
通常,当您具有测试项目时,必须将测试可执行文件链接到其余代码。通过使用CMake进行操作,您应该自动获得传递依赖。但是,为此,您需要基于CMake目标的API。
EF.Property(x, propertyName)
然后在您的测试cmake文件中,链接到与exe相同的目标,以获得对您自己的功能的访问权,包括目录和库:
add_executable(Main ... your cpp files ...)
add_library(YourLib ... your cpp files ...)
add_subdirectory(3rdparty/glfw) # or find_package(glfw)
# link YourLibs to glfw
target_link_libraries(YourLibs PUBLIC glfw)
# link Main to YourLibs and its public dependencies
target_link_libraries(Main PUBLIC YourLibs)