简体   繁体   中英

Error compiling an OpenCV project with CMake

I followed this tutorial trying to create some OpenCV project. It worked great in Windows and Visual Studio but then I tried to run it in my Ubunto VM using CMake by using the following CmakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project( TrackObj )
find_package( OpenCV REQUIRED )
add_executable( TrackObj Source.cpp Fruit.cpp Fruit.h)
target_link_libraries( TrackObj ${OpenCV_LIBS} )

When I run cmake . it seems like everything is fine:

vm@vm-ubuntu:~/Desktop/TrackObj$ cmake .
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- 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
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vm/Desktop/TrackObj

but when I run make I get the following errors:

vm@vm-ubuntu:~/Desktop/TrackObj$ make
Scanning dependencies of target TrackObj
[ 50%] Building CXX object CMakeFiles/TrackObj.dir/Source.cpp.o
In file included from /usr/include/c++/4.8/thread:35:0,
                 from /home/vm/Desktop/TrackObj/Source.cpp:10:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
make[2]: *** [CMakeFiles/TrackObj.dir/Source.cpp.o] Error 1
make[1]: *** [CMakeFiles/TrackObj.dir/all] Error 2
make: *** [all] Error 2

I'm very new to CMake but I'm pretty sure the problem is something with the fact I'm using multiple .cpp files and the way I'm using CMake. The reason is that when I tried to run a previews step in the tutorial , when the project includes only one .cpp file, it all works great.

You can see the source code that did work here (with minor changes like removing #include <opencv\\highgui.h> #include <opencv\\cv.h> and writing instead: #include <opencv2/opencv.hpp> . And the source code that did not work here with the same minor changes. In addition, the project includes the very simple Fruit.cpp and Fruit.h as described in the video.

I went over the not so friendly official tutorial of CMake and the more friendly johnlamp and OpenCV tutorials but could not find what I'm doing wrong here.

The error tells you that you need to enable C++11 features for the compiler. You can do this by setting the compiler flag -std=c++11 (or -std=c++0x for legacy compilers). In CMake, you define compiler flags in the CMAKE_C_FLAGS / CMAKE_CXX_FLAGS variables, depending on the target language.

In your case:

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM