CMake问题:链接对象文件在不同的目录



如果这是一个新手问题,请原谅。我在网上搜索了,没有找到更好的解决办法。

test.cpp正在执行测试,并利用Input.cpp中定义的类。链接器的投诉如下:">ld。输入::parse()".

  1. 将这行添加到test/CMakeLists.txt中可以工作,但是代码看起来很丑。

    target_link_libraries(测试$ {PROJECT_SOURCE_DIR}/构建/驱动程序/CMakeFiles/Driver.dir/Input.cpp.o)

  2. 尝试target_link_libraries(测试驱动程序),错误信息:(target_link_libraries):目标"驱动"可执行类型的文件不能链接到另一个目标。

    这是driver/CMakeLists.txt的导语供参考

    add_executable(Driver main.cpp Input.cpp)

  3. 不确定是否可以在driver/CMakeLists.txt中导出Input.cpp.o,然后将target_link_libraries行替换为

    target_link_libraries(测试输入)

目录树:

/CMakeLists.txt
/driver: CMakeLists.txt, main.cpp, Input.cpp, Input.hpp

/test: CMakeLists.txt
/test/gtest: test.cpp
/build/driver/CMakeFiles/Driver.dir/Input.cpp.o

任何意见都非常感谢

Input.cpp文件移动到库中。STATICOBJECT库都可以做到这一点。下面的cmake逻辑使用OBJECT库,因为它更接近问题中的第一个选项。

司机/CMakeLists.txt

add_library(driver_impl OBJECT Input.cpp Input.hpp)
add_executable(Driver main.cpp $<TARGET_OBJECTS:driver_impl>)

测试/CMakeLists.txt

add_executable(Driver test.cpp $<TARGET_OBJECTS:driver_impl>)

最新更新