0

************** EDIT-2 **************

Based on the comments, I have edited my CMakeLists file by getting rid of the automatically set variables and now it works and I am able to compile, build, and execute all three projects (both from the root directory and the individual directory). I have updated the CMakeLists files in this post in case somebody else might find it useful. Thanks for your help.

************** Original Question **************

My problem is similar to the one answered here, but that solution did not help me. I am having problems linking a project in Cmake. My directory structure is as follows:

├── CMakeLists.txt
├── build
├── armadillo_example
│   ├── include
│       ├── example.h
│   ├── src
│       ├── example.cpp
│   └── CMakeLists.txt
├── asio_opencv
│   ├── include
│       ├── asio_opencv.h
│   ├── src
│       ├── asio_opencv.cpp
│   └── CMakeLists.txt
├── boost_a-star_cities
│   ├── include
│       ├── astar-cities.h
│       ├── astar_goal_visitor.hpp
│       ├── city_writer.hpp
│       ├── distance_heuristic.hpp
│       ├── time_writer.hpp
│   ├── src
│       ├── astar_cities.cpp
│   └── CMakeLists.txt

./CMakeLists.txt

cmake_minimum_required ( VERSION 2.8 )
project ( yanack )

set ( CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build )

set ( PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include )

add_subdirectory ( armadillo_example )
add_subdirectory ( boost_a-star_cities )
add_subdirectory ( asio_opencv )

./armadillo_example/CMakeLists.txt

cmake_minimum_required ( VERSION 2.8 )
project ( armadillo_example )

set ( CMAKE_CXX_FLAGS "-std=c++11" )
set ( PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include )

find_package ( Armadillo REQUIRED )
include_directories ( ${ARMADILLO_INCLUDE_DIRS} )

file ( GLOB_RECURSE PROJ_SRCS src/*.cpp )

include_directories ( "${PROJECT_INCLUDE_DIR}" )
add_executable ( example ${PROJ_SRCS} )
target_link_libraries ( example ${ARMADILLO_LIBRARIES} )

# for compiling into shared lib
#add_library ( ${PROJECT_NAME} SHARED ${PROJECT1_SRCS} )

./asio_opencv/CMakeLists.txt

cmake_minimum_required ( VERSION 2.8 )
project ( asio_opencv )

set ( CMAKE_CXX_FLAGS "-std=c++11" )
set ( PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include )

find_package ( Boost COMPONENTS system REQUIRED )
find_package ( OpenCV REQUIRED )
include_directories ( ${OpenCV_INCLUDE_DIR} ${Boost_INCLUDE_DIR} )

file ( GLOB_RECURSE PROJ_SRCS src/*.cpp )

include_directories ( "${PROJECT_INCLUDE_DIR}" )
add_executable ( asio_opencv ${PROJ_SRCS} )
target_link_libraries ( asio_opencv ${OpenCV_LIBS} ${Boost_LIBRARIES} )

./boost_a-star_cities/CMakeLists.txt

cmake_minimum_required ( VERSION 2.8 )
project ( boost_a-star_cities )

set ( CMAKE_CXX_FLAGS "-std=c++11" )
set ( PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include )

find_package ( Boost COMPONENTS graph REQUIRED )
include_directories ( ${Boost_INCLUDE_DIR} )

file ( GLOB_RECURSE PROJ_SRCS src/*.cpp )

include_directories ( "${PROJECT_INCLUDE_DIR}" )

add_executable ( astar-cities ${PROJ_SRCS} )
target_link_libraries ( astar-cities ${Boost_LIBRARIES} )

Each of the projects have their own main functions (they will eventually be converted to libraries linked to one file containing main), and I am able to build each one individually in their own build directories. I followed the tutorial to set up this CMake structure here. When I set it up I only had armadillo_example and boost_a-star_cities and I was able to build both and get executables for both of them in the root build directory. Today I added asio_opencv (which again can be built and executed in its own build directory) but I am getting the following error:

mkdir build
cd build
cmake ..
make
Scanning dependencies of target example
[ 33%] Building CXX object armadillo_example/CMakeFiles/example.dir/src/example.cpp.o
Linking CXX executable ../example
[ 33%] Built target example
Scanning dependencies of target astar-cities
[ 66%] Building CXX object boost_a-star_cities/CMakeFiles/astar-cities.dir/src/astar-cities.cpp.o
Linking CXX executable ../astar-cities
[ 66%] Built target astar-cities
Scanning dependencies of target asio_opencv
[100%] Building CXX object asio_opencv/CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o
Linking CXX executable .
/usr/bin/ld: cannot open output file .: Is a directory
collect2: error: ld returned 1 exit status
make[2]: *** [asio_opencv] Error 1
make[1]: *** [asio_opencv/CMakeFiles/asio_opencv.dir/all] Error 2
make: *** [all] Error 2

ls
armadillo_example  asio_opencv  astar-cities  boost_a-star_cities  CMakeCache.txt  CMakeFiles  cmake_install.cmake  example  Makefile

As you can see it built the first two projects and has their executables but for the asio_opencv project , it builds it but seems to link it against the current directory (.). I'm not sure what is going wrong especially since the first two projects built and ran without any problems.

************** EDIT-1 **************

Here is the output after running make VERBOSE=1. The first one is running it from the root build directory (the one which fails). The second one is running it from asio_opencv build directory (the one which succeeds).

root build directory (fail):

make VERBOSE=1
/usr/bin/cmake -H/home/user/project-yanack -B/home/user/project-yanack/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/user/project-yanack/build/CMakeFiles /home/user/project-yanack/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/user/project-yanack/build'
make -f asio_opencv/CMakeFiles/asio_opencv.dir/build.make asio_opencv/CMakeFiles/asio_opencv.dir/depend
make[2]: Entering directory `/home/user/project-yanack/build'
cd /home/user/project-yanack/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/user/project-yanack /home/user/project-yanack/asio_opencv /home/user/project-yanack/build /home/user/project-yanack/build/asio_opencv /home/user/project-yanack/build/asio_opencv/CMakeFiles/asio_opencv.dir/DependInfo.cmake --color=
Dependee "/home/user/project-yanack/build/asio_opencv/CMakeFiles/asio_opencv.dir/DependInfo.cmake" is newer than depender "/home/user/project-yanack/build/asio_opencv/CMakeFiles/asio_opencv.dir/depend.internal".
Dependee "/home/user/project-yanack/build/asio_opencv/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/user/project-yanack/build/asio_opencv/CMakeFiles/asio_opencv.dir/depend.internal".
Scanning dependencies of target asio_opencv
make[2]: Leaving directory `/home/user/project-yanack/build'
make -f asio_opencv/CMakeFiles/asio_opencv.dir/build.make asio_opencv/CMakeFiles/asio_opencv.dir/build
make[2]: Entering directory `/home/user/project-yanack/build'
/usr/bin/cmake -E cmake_progress_report /home/user/project-yanack/build/CMakeFiles 1
[100%] Building CXX object asio_opencv/CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o
cd /home/user/project-yanack/build/asio_opencv && /usr/bin/c++    -std=c++11 -I/home/user/project-yanack/include -I/home/user/project-yanack -I/usr/include/opencv -I/home/user/project-yanack/build/asio_opencv -I/home/user/project-yanack/asio_opencv/include    -o CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o -c /home/user/project-yanack/asio_opencv/src/asio_opencv.cpp
Linking CXX executable .
cd /home/user/project-yanack/build/asio_opencv && /usr/bin/cmake -E cmake_link_script CMakeFiles/asio_opencv.dir/link.txt --verbose=1
/usr/bin/c++   -std=c++11    CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o  -o . -rdynamic /usr/lib/x86_64-linux-gnu/libopencv_videostab.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ts.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_superres.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_stitching.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ocl.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_gpu.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_contrib.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8 -lboost_system /usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8 
make[2]: Leaving directory `/home/user/project-yanack/build'
make[1]: Leaving directory `/home/user/project-yanack/build'
/usr/bin/ld: cannot open output file .: Is a directory
collect2: error: ld returned 1 exit status
make[2]: *** [asio_opencv] Error 1
make[1]: *** [asio_opencv/CMakeFiles/asio_opencv.dir/all] Error 2
make: *** [all] Error 2

asio_opencv build directory (success):

make VERBOSE=1
/usr/bin/cmake -H/home/user/project-yanack/asio_opencv -B/home/user/project-yanack/asio_opencv/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/user/project-yanack/asio_opencv/build/CMakeFiles /home/user/project-yanack/asio_opencv/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/user/project-yanack/asio_opencv/build'
make -f CMakeFiles/asio_opencv.dir/build.make CMakeFiles/asio_opencv.dir/depend
make[2]: Entering directory `/home/user/project-yanack/asio_opencv/build'
cd /home/user/project-yanack/asio_opencv/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/user/project-yanack/asio_opencv /home/user/project-yanack/asio_opencv /home/user/project-yanack/asio_opencv/build /home/user/project-yanack/asio_opencv/build /home/user/project-yanack/asio_opencv/build/CMakeFiles/asio_opencv.dir/DependInfo.cmake --color=
Dependee "/home/user/project-yanack/asio_opencv/build/CMakeFiles/asio_opencv.dir/DependInfo.cmake" is newer than depender "/home/user/project-yanack/asio_opencv/build/CMakeFiles/asio_opencv.dir/depend.internal".
Dependee "/home/user/project-yanack/asio_opencv/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/user/project-yanack/asio_opencv/build/CMakeFiles/asio_opencv.dir/depend.internal".
Scanning dependencies of target asio_opencv
make[2]: Leaving directory `/home/user/project-yanack/asio_opencv/build'
make -f CMakeFiles/asio_opencv.dir/build.make CMakeFiles/asio_opencv.dir/build
make[2]: Entering directory `/home/user/project-yanack/asio_opencv/build'
/usr/bin/cmake -E cmake_progress_report /home/user/project-yanack/asio_opencv/build/CMakeFiles 1
[100%] Building CXX object CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o
/usr/bin/c++    -std=c++11 -I/usr/include/opencv -I/home/user/project-yanack/asio_opencv/build -I/home/user/project-yanack/asio_opencv/include    -o CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o -c /home/user/project-yanack/asio_opencv/src/asio_opencv.cpp
Linking CXX executable asio_opencv
/usr/bin/cmake -E cmake_link_script CMakeFiles/asio_opencv.dir/link.txt --verbose=1
/usr/bin/c++   -std=c++11    CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o  -o asio_opencv -rdynamic /usr/lib/x86_64-linux-gnu/libopencv_videostab.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ts.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_superres.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_stitching.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ocl.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_gpu.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_contrib.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8 -lboost_system /usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8 
make[2]: Leaving directory `/home/user/project-yanack/asio_opencv/build'
/usr/bin/cmake -E cmake_progress_report /home/user/project-yanack/asio_opencv/build/CMakeFiles  1
[100%] Built target asio_opencv
make[1]: Leaving directory `/home/user/project-yanack/asio_opencv/build'
/usr/bin/cmake -E cmake_progress_start /home/user/project-yanack/asio_opencv/build/CMakeFiles 0
Community
  • 1
  • 1
shaun
  • 560
  • 1
  • 11
  • 29
  • Why are you using `file ( GLOB_RECURSE` for a single file? Can you execute `make VERBOSE=1`. Curious, that the executable is called `.` – usr1234567 Dec 29 '15 at 07:43
  • Modifing automatic variables `CMAKE_BINARY_DIR` and `PROJECT_SOURCE_DIR` is not a good idea. `but for the asio_opencv project ... but seems to link it against the current directory` - as @usr1234567 notes, cmake uses `.` as the **name of the executable itself**. It looks like someone set `OUTPUT_NAME` property for it. – Tsyvarev Dec 29 '15 at 07:47
  • @usr1234567: I am using glob_recurse because eventually I will have multiple cpp files as the project grows. This way I don't need to manually add the file names in. – shaun Dec 29 '15 at 12:45
  • @Tsyvarev: I followed the linked tutorial and am not sure what the correct procedure is in terms of setting automatic variables. I am open to suggestion. – shaun Dec 29 '15 at 12:46
  • Actually, the only comment to the tutorial you use also notes about setting automatic variables. You may found it useful. In your case origin of automatic variables you **set** doesn't have any meaning - simply rename there variables into you own ones. And try to rebuild you project from the **clean build directory**. – Tsyvarev Dec 29 '15 at 13:23
  • In the last modification you remove *almost all* usage of CMake variables. This is not required. You need only to remove **assigning** (`set`) of **automatic** variables `CMAKE_BINARY_DIR` and `PROJECT_SOURCE_DIR`. You are free to *use* (read value of) these variables in your code. Also you are free to *assign other CMake variables with special meaning*, like `EXECUTABLE_OUTPUT_PATH` - this variable is exported special for possible assignment. – Tsyvarev Dec 29 '15 at 14:13
  • 1
    **Answering** to the question **in the question post itself is discouraged** on Stack Overflow. Better create answer post, and describe there how you have resolved your problem. Also, revert question post to the question form. It is completely OK to self-answer your question. You may also accept your answer, so question will be marked as answered in a usual way. – Tsyvarev Dec 29 '15 at 14:19

0 Answers0