【问题标题】:Cmake: cannot open output file no such file or directoryCmake:无法打开输出文件没有这样的文件或目录
【发布时间】:2016-09-15 15:52:40
【问题描述】:

我正在学习将 cmake 用于使用共享库的项目,但我不断收到此错误:

Linking CXX executable test/test/robot_test
/usr/bin/ld: cannot open output file test/test/robot_test: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [test/test/robot_test] Error 1
make[1]: *** [CMakeFiles/test/robot_test.dir/all] Error 2
make: *** [all] Error 2

这是我的 CMake 文件:

cmake_minimum_required(VERSION 2.8.12)
project("Particle Filter")
set(CMAKE_BUILD_TYPE Release)
include_directories(include)

set(LIB_SOURCES src/robot.cc src/sampler.cc src/general.cc)
set(LIB_HEADERS include/robot.h include/sampler.h include/general.h)

add_library(my_lib SHARED ${LIB_SOURCES} ${LIB_HEADERS})
install(TARGETS my_lib DESTINATION lib)

set(APP_SOURCES test/robot_test.cc test/sampler_test.cc)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test")

foreach(test ${APP_SOURCES})
    #cut off .cc of src files using empty string
    string(REPLACE ".cc" "" testname ${test})
    add_executable(${testname} ${test})
    target_link_libraries(${testname} my_lib)
endforeach(test ${APP_SOURCES})

add_definitions(
    -std=c++11 # Or -std=c++0x
    # Other flags
)

这是我的树(不包括包含很多此类内容的构建目录,例如我的 makefile、.so 和 .a 文件):

├── CMakeLists.txt
├── driver.cc
├── include
│   ├── general.h
│   ├── robot.h
│   └── sampler.h
├── lib
├── notes
├── src
│   ├── general.cc
│   ├── robot.cc
│   └── sampler.cc
└── test
    ├── robot_test.cc
    └── sampler_test.cc

另外,.so 或 .a 文件在 sudo make install 后没有保存到我的 lib 文件夹中,我该如何解决这个问题?

【问题讨论】:

    标签: c++ makefile cmake


    【解决方案1】:

    命令add_executable 的第一个参数不是文件名,而是目标 的名称。目标名称不应包含斜杠(“/”)等特殊符号。

    您可以控制通过设置变量CMAKE_RUNTIME_OUTPUT_DIRECTORY创建的可执行文件的输出目录:

    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test")
    ...
    # Relative path to executable will be 'test/robot_test'.
    add_executable(robot_test robot_test.cc)
    

    【讨论】:

    • 它给了我新的错误:/usr/bin/ld: 无法打开输出文件 test/test/robot_test: 没有这样的文件或目录 collect2: 错误: ld 返回 1 退出状态 make[2]: *** [test/test/robot_test] 错误 1 ​​make[1]: *** [CMakeFiles/test/robot_test.dir/all] 错误 2 make: *** [all] 错误 2
    • 编辑您的问题并展示您如何修改代码。
    • 我更新了。它在我的构建文件夹中创建了一个空的“测试”文件夹。
    • 您仍然使用test/robot_test 作为目标 名称。应该是robot_test
    • 由于您的两个源都位于同一目录中,您可以将它们的名称存储在变量APP_SOURCES 中,而无需目录:robot_test.cc sampler_test.cc。创建可执行文件将更改为add_executable(${testname} test/${test})。更复杂的方法是使用get_filename_component 处理文件的每个路径,并将目录组件分配给创建目标的属性 RUNTIME_OUTPUT_DIRECTORY
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2019-04-07
    • 2017-05-04
    相关资源
    最近更新 更多