gpt4 book ai didi

c++ - CMake链接另一个项目中的单个类

转载 作者:太空狗 更新时间:2023-10-29 21:20:34 25 4
gpt4 key购买 nike

它是我的第一个 C++ 项目之一,我在使用 CMake 时遇到了问题。

我有一个带有可执行文件的 MyProject,我有一个带有提升单元测试的项目测试。我尝试了以下方式,但我失败了。显然,我不能以这种方式拥有两个可执行文件,而且我不知道如何修复它。

这是MyProject的CMake

project (MyProject)
find_package( Boost 1.48.0 COMPONENTS thread )
set(MYPROJECT_SRCS main.cpp foo.h foo.cpp)
add_executable(MyProject ${MYPROJECT_SRCS})
target_link_libraries(MyProject ${Boost_LIBRARIES})

这是测试的 CMake

project (tests)
find_package( Boost 1.48.0 COMPONENTS thread unit_test_framework)
find_package( Boost 1.48.0 COMPONENTS thread )
include_directories("../MyProject")
set(TEST_SRCS test.cpp )
add_executable(tests ${TEST_SRCS})
target_link_libraries(tests ${Boost_LIBRARIES} MyProject)
add_test( example_test tests )

CMake Error at tests/CMakeLists.txt:13 (target_link_libraries):
Target "MyProject" of type EXECUTABLE may not be linked into another
target. One may link only to STATIC or SHARED libraries, or to executables with the ENABLE_EXPORTS property set.

我尝试“ENABLE_EXPORTS 属性集”,但我认为我做错了。

最佳答案

你不应该将你的可执行文件与测试链接起来,相反你需要在测试源列表中包含你的主项目的源文件:

set(TEST_SRCS test.cpp ../MyProject/foo.cpp)

target_link_libraries(测试 ${Boost_LIBRARIES} )

附言当您想分析测试覆盖率时,它也会很有用。

关于c++ - CMake链接另一个项目中的单个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24313760/

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