【问题标题】:OpenCV undefined reference toOpenCV 未定义的引用
【发布时间】:2018-03-21 14:50:45
【问题描述】:

我使用的是 Ubuntu 16.04 和 OpenCV 2.4.9,而我非常简单的示例程序总是无法构建。我正在构建 CLion。

由于我是 OpenCV 的新手,我有一些问题可以缩小问题的范围。我尝试了两个不同的程序。

第一个是这个:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main(int argc, char **argv) {
    IplImage *img = cvLoadImage(argv[1]);
    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);
    cvWaitKey(0);
    cvReleaseImage(&img);
    cvDestroyWindow("Example1" );
}

失败的原因是:

Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
CMakeFiles/untitled.dir/main.cpp.o: In function `main':
/home/computervision/CLionProjects/untitled/main.cpp:7: undefined reference to `cvLoadImage'
/home/computervision/CLionProjects/untitled/main.cpp:8: undefined reference to `cvNamedWindow'
/home/computervision/CLionProjects/untitled/main.cpp:9: undefined reference to `cvShowImage'
/home/computervision/CLionProjects/untitled/main.cpp:10: undefined reference to `cvWaitKey'
/home/computervision/CLionProjects/untitled/main.cpp:11: undefined reference to `cvReleaseImage'
/home/computervision/CLionProjects/untitled/main.cpp:12: undefined reference to `cvDestroyWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled.dir/build.make:94: recipe for target 'untitled' failed
make[3]: *** [untitled] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/untitled.dir/all' failed
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/untitled.dir/rule' failed
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
Makefile:118: recipe for target 'untitled' failed
make: *** [untitled] Error 2

我的第二个是这样的:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main( int argc, char** argv ) {

    cv::Mat image;
    image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR);

    if(! image.data ) {
        std::cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
    cv::imshow( "Display window", image );

    cv::waitKey(0);
    return 0;
}

失败的原因是:

Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
CMakeFiles/untitled.dir/main.cpp.o: In function `main':
/home/computervision/CLionProjects/untitled/main.cpp:9: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/computervision/CLionProjects/untitled/main.cpp:16: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/computervision/CLionProjects/untitled/main.cpp:17: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
/home/computervision/CLionProjects/untitled/main.cpp:17: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/home/computervision/CLionProjects/untitled/main.cpp:19: undefined reference to `cv::waitKey(int)'
CMakeFiles/untitled.dir/main.cpp.o: In function `cv::Mat::~Mat()':
/usr/local/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
CMakeFiles/untitled.dir/main.cpp.o: In function `cv::Mat::operator=(cv::Mat const&)':
/usr/local/include/opencv2/core/mat.hpp:298: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
CMakeFiles/untitled.dir/main.cpp.o: In function `cv::Mat::release()':
/usr/local/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled.dir/build.make:94: recipe for target 'untitled' failed
make[3]: *** [untitled] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/untitled.dir/all' failed
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/untitled.dir/rule' failed
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
Makefile:118: recipe for target 'untitled' failed
make: *** [untitled] Error 2

我的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(untitled)

set(CMAKE_CXX_STANDARD 11)

add_executable(untitled main.cpp)

我想知道我做错了什么。 大多数搜索都会导致 g++ 命令的设置错误,但我认为 CLion 知道它在做什么(我也尝试使用该命令,同样的错误)。 例如这里:openCV error : undefined reference to `cvLoadImage' Ubuntu

我假设highgui 没有正确链接。可能是这种情况,我的输出:

computervision@computervision-VirtualBox:~/ex$ pkg-config opencv --libs
/usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_ml.so

看起来有点糟糕,我想我在安装过程中犯了一个错误(只是哪个?)。

另一个错误可能是 CMakeLists.txt。 Here 建议改一下。但是这条线

LIBRARIES += glog gflags protobuf leveldb snappy 

导致错误。

执行中

export CMAKE_CXX_FLAGS=`pkg-config opencv --cflags --libs`

在启动 CLion 之前也没有任何区别。

编辑: 使用 cmake:

computervision@computervision-VirtualBox:~/CLionProjects/untitled$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
OpenCV_LIBS: opencv_mlopencv_flannopencv_core
-- Configuring done
-- Generating done
-- Build files have been written to: /home/computervision/CLionProjects/untitled
computervision@computervision-VirtualBox:~/CLionProjects/untitled$ g++ -o untitled main.cpp `pkg-config opencv --cflags --libs` 
/tmp/ccdAr2hr.o: In function `main':
main.cpp:(.text+0x78): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
main.cpp:(.text+0x128): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
main.cpp:(.text+0x1a2): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
main.cpp:(.text+0x1ca): undefined reference to `cv::waitKey(int)'
collect2: error: ld returned 1 exit status

编辑(2): 在cmake 之后使用make

computervision@computervision-VirtualBox:~/CLionProjects/untitled$ make 
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
CMakeFiles/untitled.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x78): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
main.cpp:(.text+0x128): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
main.cpp:(.text+0x1a2): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
main.cpp:(.text+0x1ca): undefined reference to `cv::waitKey(int)'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled.dir/build.make:97: recipe for target 'untitled' failed
make[2]: *** [untitled] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/untitled.dir/all' failed
make[1]: *** [CMakeFiles/untitled.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

【问题讨论】:

  • 您缺少链接器参数中的 videoio 和 highgui 库
  • 根据您的 CMakeLists.txt,您根本没有链接任何东西。 TARGET_LINK_LIBRARIES 命令在哪里?
  • 按照@nayab 的建议添加了两者。没有变化
  • 当 v3.1+ 可用时为什么要使用 v2.4.9?
  • 通常"out-of-source" 构建是首选,因为它们不会将您的所有构建内容与受版本控制的源代码混合。因此,您将在包含 CMakeLists.txt 的目录下创建一个名为 build 的子目录,然后执行 cd build &amp;&amp; cmake ..

标签: c++ opencv clion


【解决方案1】:

CMakeLists.txt 中的链接 OpenCV 丢失。

cmake_minimum_required(VERSION 3.9)
project(untitled)

set(CMAKE_CXX_STANDARD 11)
find_package( OpenCV REQUIRED ) # locate OpenCV in system
include_directories( ${OpenCV_INCLUDE_DIRS} ) # provide library headers
add_executable(untitled main.cpp)
target_link_libraries( untitled ${OpenCV_LIBS} /usr/lib/x86_64-linux-gnu/libopencv_highgui.so) # link OpenCV libraries , hightgui.so not found by cmake so this hack
MESSAGE("OpenCV_LIBS: " ${OpenCV_LIBS} )  #display opencv libs found

如果您的编译器找到 OpenCV 并在执行 cmake 后应该显示找到的 OpenCV 库。

【讨论】:

  • 无变化。尝试用 CLion 和 g++ 编译它。
  • cmake 找到 OpenCV 库了吗?
  • 恐怕我不太明白。如前所述,我对 OpenCV 很陌生
  • 执行 cmake 命令后,是否显示库,因为我们添加了一条消息以显示找到的库。
  • 从“pkg-config opencv --cflags --libs”的输出中可以看到,它只显示了 3 个库。我怀疑它的安装问题。但是,在 CMakeLists.txt 中添加 highgui 库后,它应该可以工作。你能删除“CMakeCache.txt”并执行“cmake”吗?和“制作”
猜你喜欢
  • 2012-06-23
  • 1970-01-01
  • 2012-05-01
  • 2016-04-02
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多