OpenCV未定义对

时间:2018-03-18 22:33:50

标签: c++ opencv clion

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

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

1号是这一个:

#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 

导致错误。

执行

  

导出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): 在make之后使用cmake

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

1 个答案:

答案 0 :(得分:2)

缺少OpenCV中的CMakeLists.txt链接。

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库。