c++ - CMake 错误 : linker command failed with exit code 1 and cpp. o 文件

标签 c++ opencv makefile cmake linker-errors

我正在使用 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/

相关文章:

c++ - QScrollArea 内的 QGridLayout 内的方形自定义 QWidget

c++ - 用于大规模数据处理的标准 C++ 库

c++ - std::thread 不是使用 Eclipse Kepler MinGW 命名空间 std 的成员

linux - 如何配置 makefile?

c - 如何将新的源文件添加到 glibc makefile 中?

c++ - 错误: cast from ‘void*’ to ‘int’ loses precision [-fpermissive] in makefile

c++ - gsl 库中的 span 和 array_view 有什么区别?

xml - 用于手势识别的 Haar 训练数据集 - 计数

python - 如何自动裁剪图片中灰色内的白色部分?

python - 在不改变纵横比的情况下设置分辨率