我正在使用 CMake 编译一个小项目。
这是我在 CMakeLists.txt 中编写的内容:
cmake_minimum_required(VERSION 3.2)
set (CMAKE_CXX_STANDARD 11)
project(DAF)
find_package(OpenCV REQUIRED)
include_directories(include)
include_directories(${OpenCV_INCLUDE_DIRS} )
file(GLOB Src_Sources "src/*.cpp")
file(GLOB Test_Sources "test/*.cpp")
add_executable(executable ${Src_Sources} ${Test_Sources})
target_link_libraries( executable include ${OpenCV_LIBS} )
我有两个目录 src 和 test。 src 目录仅包含保存函数的文件,而 test 目录包含主文件。
一旦我使用 cmake
命令,然后使用 make
命令,我就会收到此错误:
Scanning dependencies of target executation
[ 25%] Building CXX object CMakeFiles/executation.dir/src/image.cpp.o
[ 50%] Building CXX object CMakeFiles/executation.dir/test/starter_1.cpp.o
[ 75%] Linking CXX executable executation
ld: library not found for -linclude
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [executation] Error 1
make[1]: *** [CMakeFiles/executation.dir/all] Error 2
make: *** [all] Error 2
所以我觉得奇怪的是它创建了 .cpp.o
文件。这正常吗?
然后如何修复错误找不到 -linclude 库
?
最佳答案
是的,CMake 采取源文件的名称,并附加一个 .o
是正常的编译各自的目标文件时。例如源文件image.cpp
将被编译为image.cpp.o
。 CMake 生成的 Makefile 将包含唯一的目标 <someSourceFileName>.o
对于每个源文件,其中 <someSourceFileName>
可以是.cc
, .cpp
, .c
等
错误:
ld: library not found for -linclude
表示您已尝试链接名为 include
的库到可执行文件。这可能不是您想要的。出现include
实际上是您的包含目录,并且您已经使用此行在 CMake 中指定了包含目录:
include_directories(include)
要消除错误,只需取出 include
来自 target_link_libraries()
命令,如下所示:
target_link_libraries( executable ${OpenCV_LIBS} )
关于c++ - CMake 错误 : linker command failed with exit code 1 and cpp. o 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59654144/