Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OpenCV未定义对的引用_C++_Opencv_Clion - Fatal编程技术网

C++ OpenCV未定义对的引用

C++ OpenCV未定义对的引用,c++,opencv,clion,C++,Opencv,Clion,我使用的是Ubuntu 16.04和OpenCV 2.4.9,我的简单示例程序经常无法构建。我在建克莱恩的房子 由于我是OpenCV的新手,我有一些问题需要解决。我尝试了两种不同的程序 第一个是这个: #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hp

我使用的是Ubuntu 16.04和OpenCV 2.4.9,我的简单示例程序经常无法构建。我在建克莱恩的房子

由于我是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" );
}
我的第二个是:

#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;
}
我不知道我做错了什么。 大多数搜索都会导致g++命令设置错误,但我认为CLion知道它在做什么(我也尝试过使用该命令,同样的错误)。 例如:

我假设
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。建议对其进行更改。但是这条线

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

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

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

根据CMakeLists.txt,链接器args中缺少videoio和highgui库,您根本没有链接任何东西。
TARGET\u LINK\u LIBRARIES
命令在哪里?按照@nayab的建议添加了这两个命令。没有变化为什么在v3.1+可用时使用v2.4.9?通常情况下,首选“源代码外”版本,因为它们不会将所有的构建内容与版本控制下的源代码混合在一起。因此,您可以在包含
CMakeLists.txt
的目录下创建一个名为
build
的子目录,然后执行
cd build&&cmake..
链接器args中的videoio和highgui库基于您的CMakeLists.txt,您根本没有链接任何东西。
TARGET\u LINK\u LIBRARIES
命令在哪里?按照@nayab的建议添加了这两个命令。没有变化为什么在v3.1+可用时使用v2.4.9?通常情况下,首选“源代码外”版本,因为它们不会将所有的构建内容与版本控制下的源代码混合在一起。因此,您可以在包含
CMakeLists.txt
的目录下创建一个名为
build
的子目录,然后执行
cd build&&cmake..
无更改。试图用CLion和g++编译它。cmake找到OpenCV库了吗?恐怕我不太明白。如前所述,在执行cmake命令后,我对opencv非常陌生,它是否显示了库,因为我们添加了一条消息来显示找到的libs。正如您从“pkg config opencv--cflags--libs”的输出中所看到的,它只显示了3个库。我怀疑它的安装有问题。但是,在CMakeLists.txt中添加highgui库后,它应该可以工作。你能删除“CMakeCache.txt”并执行“cmake.”和“make”操作吗。试图用CLion和g++编译它。cmake找到OpenCV库了吗?恐怕我不太明白。如前所述,在执行cmake命令后,我对opencv非常陌生,它是否显示了库,因为我们添加了一条消息来显示找到的libs。正如您从“pkg config opencv--cflags--libs”的输出中所看到的,它只显示了3个库。我怀疑它的安装有问题。但是,在CMakeLists.txt中添加highgui库后,它应该可以工作。你能删除“CMakeCache.txt”并执行“cmake.”和“make”吗
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
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
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