CMake链接错误(collect2:ld返回1个退出状态链接到当前目录。)

时间:2015-12-29 01:04:01

标签: c++ cmake

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

根据评论,我已经编辑了我的CMakeLists文件,删除了自动设置的变量,现在它可以工作了,我能够编译,构建和执行所有三个项目(从根目录和单个目录)。我已经在这篇文章中更新了CMakeLists文件,以防其他人发现它有用。谢谢你的帮助。

************** 原始问题 **************

我的问题类似于回答here的问题,但该解决方案对我没有帮助。我在连接Cmake的项目时遇到问题。我的目录结构如下:

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

每个项目都有自己的主要功能(它们最终将转换为链接到包含main的一个文件的库),并且我能够在它们自己的构建目录中单独构建每个项目。我按照教程设置了这个CMake结构here。当我设置它时,我只有armadillo_example和boost_a-star_cities,我能够在根构建目录中构建两者并获取它们的可执行文件。今天我添加了asio_opencv(它可以在自己的构建目录中构建和执行)但是我收到以下错误:

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

正如你所看到它构建了前两个项目并且具有可执行文件但是对于asio_opencv项目,它构建它但似乎将它链接到当前目录(。)。我不确定会出现什么问题,特别是因为前两个项目的构建和运行没有任何问题。

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

这是运行make VERBOSE = 1后的输出。第一个是从根构建目录(失败的目录)运行它。第二个是从asio_opencv构建目录(成功的目录)运行它。

根构建目录(失败):

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构建目录(成功):

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

0 个答案:

没有答案