Dobob Dobob - 2 months ago 20
C++ Question

Cmake: cannot open output file no such file or directory

I am learning to use cmake for a project using a shared library, but I keep getting this error:

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


Here is my CMake file:

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
)


Here is my tree (excluding the build directory that contains a lot of this such as my makefile,.so and .a file):

├── 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


Also, the .so or .a files are not getting saved to my lib folder after sudo make install, how do I fix this?

Answer

The first parameter for command add_executable is not a filename but a name of target. Target's name shouldn't contain special symbols like slash ("/").

You may control output directory of executables created by setting variable 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)