Cmake:无法打开输出文件没有这样的文件或目录

时间:2016-09-15 15:52:40

标签: c++ makefile cmake

我正在学习使用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

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

1 个答案:

答案 0 :(得分:0)

命令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)