CMake error: linker command failed with exit code 1 and cpp.o files

CMake error: linker command failed with exit code 1 and cpp.o files

我正在使用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 个文件。这正常吗?

然后如何修复错误 library not found for -linclude

是的,CMake取源文件名,在编译各个目标文件的时候附加一个.o是正常的。比如源文件image.cpp会被编译成image.cpp.o。 CMake 生成的 Makefile 将为每个源文件包含一个唯一的目标 <someSourceFileName>.o,其中 <someSourceFileName> 可以是 .cc.cpp.c

错误:

ld: library not found for -linclude

表示您已尝试 link 一个名为 include 的库到可执行文件。这可能不是您想要的。看起来 include 实际上是您的包含目录,并且您已经在您的 CMake 中使用以下行指定了包含目录:

include_directories(include)

要消除错误,只需从 target_link_libraries() 命令中取出 include,如下所示:

target_link_libraries( executable ${OpenCV_LIBS} )