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

标签 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 文件夹中,我该如何解决这个问题?

最佳答案

命令的第一个参数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)

关于c++ - Cmake:无法打开没有这样的文件或目录的输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39515420/

相关文章:

visual-studio-2010 - 在Windows 7,Visual Studio 2010和命令行中使用CMake

c++ - VS 2017 使用交叉编译器构建 x64 项目

c++ - Qt 和 C++ 的报告库

regex - 在makefile中,如何获取从一个绝对路径到另一个绝对路径的相对路径?

c - 如何让编译器识别对库的引用?

c - DLL 对静态库的依赖

c++ - 统一调节程序执行率[Windows C++]

c++ - 游戏对象向左和向上移动,但不向右和向下移动

c++ - C++中许多具有相同结构的成员函数

makefile - 使用clearmake时,MAKEFILE_LIST是一个空列表