CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件

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

本文关键字:代码 cpp 文件 退出 错误 链接 失败 命令 CMake      更新时间:2023-10-16

我正在使用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 和测试。src 目录仅包含保存函数的文件,而测试目录包含主文件。

一旦我使用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 生成的生成文件将包含每个源文件的唯一目标<someSourceFileName>.o,其中<someSourceFileName>可以是.cc.cpp.c等。

错误:

ld: library not found for -linclude

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

include_directories(include)

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

target_link_libraries( executable ${OpenCV_LIBS} )