Dobob Dobob - 1 year ago 308
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(LIB_SOURCES src/ src/ src/
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/ 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})

-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
├── include
│   ├── general.h
│   ├── robot.h
│   └── sampler.h
├── lib
├── notes
├── src
│   ├──
│   ├──
│   └──
└── test

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

Answer Source

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:

# Relative path to executable will be 'test/robot_test'.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download