CMake - OpenCV - 找不到-lDisplayImage

时间:2017-12-07 17:50:00

标签: c++ opencv cmake

我尝试使用opencv使用Cmake在C ++上构建项目。但是有一个我无法解决的错误。我搜索了很多,但一无所获。

注意:
我是OpenCV的初学者。所以我甚至不知道为什么这个图书馆需要。只是对网站做了相同的介绍。

错误是/usr/bin/ld: cannot find -lDisplayImage

我的code

#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
    // read image
    Mat img = imread("foo.jpg", CV_LOAD_IMAGE_UNCHANGED);

    // show it in the window
    imshow("Window Name",img);

    waitKey();
}

我的CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(image_processing)

find_package( OpenCV REQUIRED )

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(image_processing ${SOURCE_FILES})
target_link_libraries( image_processing DisplayImage ${OpenCV_LIBS} )

我的编译输出:

[ 50%] Linking CXX executable image_processing
/usr/bin/ld: cannot find -lDisplayImage
collect2: error: ld returned 1 exit status
CMakeFiles/image_processing.dir/build.make:137: recipe for target 'image_processing' failed
make[3]: *** [image_processing] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/image_processing.dir/all' failed
make[2]: *** [CMakeFiles/image_processing.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/image_processing.dir/rule' failed
make[1]: *** [CMakeFiles/image_processing.dir/rule] Error 2
Makefile:118: recipe for target 'image_processing' failed
make: *** [image_processing] Error 2

ld -ldisplayimage --verbose命令的输出:

attempt to open //usr/local/lib/x86_64-linux-gnu/libdisplayimage.so failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libdisplayimage.a failed
attempt to open //lib/x86_64-linux-gnu/libdisplayimage.so failed
attempt to open //lib/x86_64-linux-gnu/libdisplayimage.a failed
attempt to open //usr/lib/x86_64-linux-gnu/libdisplayimage.so failed
attempt to open //usr/lib/x86_64-linux-gnu/libdisplayimage.a failed
attempt to open //usr/local/lib64/libdisplayimage.so failed
attempt to open //usr/local/lib64/libdisplayimage.a failed
attempt to open //lib64/libdisplayimage.so failed
attempt to open //lib64/libdisplayimage.a failed
attempt to open //usr/lib64/libdisplayimage.so failed
attempt to open //usr/lib64/libdisplayimage.a failed
attempt to open //usr/local/lib/libdisplayimage.so failed
attempt to open //usr/local/lib/libdisplayimage.a failed
attempt to open //lib/libdisplayimage.so failed
attempt to open //lib/libdisplayimage.a failed
attempt to open //usr/lib/libdisplayimage.so failed
attempt to open //usr/lib/libdisplayimage.a failed
attempt to open //usr/x86_64-linux-gnu/lib64/libdisplayimage.so failed
attempt to open //usr/x86_64-linux-gnu/lib64/libdisplayimage.a failed
attempt to open //usr/x86_64-linux-gnu/lib/libdisplayimage.so failed
attempt to open //usr/x86_64-linux-gnu/lib/libdisplayimage.a failed
ld: cannot find -ldisplayimage

我试图找到那个图书馆,但我无法找到。我检查了所有lib个文件夹。

我的opencv安装在/opt/opencv/上并构建于/opt/opencv/release/

感谢您的帮助。我对如何解决它一无所知。

1 个答案:

答案 0 :(得分:1)

当您从示例中复制并粘贴而不理解它们时会发生这种情况。问题出在CMakeLists.txt

你有

with lock, contextlib.closing(sqlite3.connect(self.dbName)) as connection:
    # Do Somehing

我会一行一行地去。首先,它检查你有一个最小的cmake版本(以确保之后的一些函数是有效的),然后显然是项目的名称,它可以是你想要的任何。然后你找到OpenCV库,它设置了几个变量(包括,libs等)。然后你设置c ++ 11标准和你的源文件,在这种情况下,它只有1个用于THIS可执行文件(你可以根据需要添加多个,如果需要,可以使用.h)。

最后重要的部分,你传递2个参数的add_executable,可执行文件的名称(可以是任何东西)和源文件......你甚至可以手动编写源文件,而不是先将它们设置为变量。之后你说哪些库将链接到你的可执行文件......这里是你有错误的地方!您必须将您之前声明的可执行文件的相同名称作为第一个参数,然后使用所使用的库。当您执行cmake_minimum_required(VERSION 3.8) project(image_processing) find_package( OpenCV REQUIRED ) set(CMAKE_CXX_STANDARD 11) set(SOURCE_FILES main.cpp) add_executable(image_processing ${SOURCE_FILES}) target_link_libraries( image_processing DisplayImage ${OpenCV_LIBS} ) 时,它会在find_package(OpenCV REQUIRED)变量中设置库名称。如果您需要另一个库,您可以手动输入名称,并在编译时链接它。在这种情况下,您有名称DisplayImage,它认为它是一个库名称并尝试链接它,这意味着编译时$(OpenCV_LIBS)

这意味着您删除了它,它应该编译:)

-lDisplayImage

此外,在找到OpenCV包之后,我建议你做

target_link_libraries( image_processing  ${OpenCV_LIBS} )

这将确保将include文件夹添加到编译中。在gcc中等效于-Ifolder / path选项。如果必须包含来自不同库项目的更多文件夹,则可以使用此命令。

我希望这会对你有所帮助。