20

I'm trying to compile the new version of OpenCV 3.3 (released on Aug 3, 2017) but I'm getting an error of C++11

This is my cmake command line:

cmake -D CMAKE_BUILD_TYPE=RELEASE \ 
-D CMAKE_INSTALL_PREFIX=/usr/local \    
-D WITH_CUDA=ON \    
-D ENABLE_FAST_MATH=1 \    
-D CUDA_FAST_MATH=1 \    
-D WITH_CUBLAS=1 \    
-DINSTALL_C_EXAMPLES=OFF \    
-D INSTALL_PYTHON_EXAMPLES=ON \    
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.3/modules \    
-D BUILD_SHARED_LIBS=ON \    
-D WITH_GTK=ON /    
-D BUILD_EXAMPLES=ON ..

I've edited CMakeLists by turning ON some components (even without making any changes, the error remain the same):

OCV_OPTION(WITH_OPENGL         "Include OpenGL support"                      ON  IF (NOT ANDROID AND NOT WINRT) )
OCV_OPTION(WITH_OPENVX         "Include OpenVX support"                      ON)
OCV_OPTION(WITH_OPENNI         "Include OpenNI support"                      ON  IF (NOT ANDROID AND NOT IOS AND NOT WINRT) )
OCV_OPTION(WITH_OPENNI2        "Include OpenNI2 support"                     ON  IF (NOT ANDROID AND NOT IOS AND NOT WINRT) )
OCV_OPTION(WITH_OPENMP         "Include OpenMP support"                      ON)
OCV_OPTION(WITH_OPENCL_SVM     "Include OpenCL Shared Virtual Memory support" ON ) # experimental

And here my CMakeError.log file:

Compilation failed:
    source file: '/home/jhros/opencv-3.3.0/cmake/checks/cxx11.cpp'
    check option: ''
===== BUILD LOG =====
Change Dir: /home/jhros/opencv-3.3.0/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/make" "cmTC_f915e/fast"
/usr/bin/make -f CMakeFiles/cmTC_f915e.dir/build.make CMakeFiles/cmTC_f915e.dir/build
make[1]: Entering directory '/home/jhros/opencv-3.3.0/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_f915e.dir/cxx11.cpp.o
/usr/bin/c++      -o CMakeFiles/cmTC_f915e.dir/cxx11.cpp.o -c /home/jhros/opencv-3.3.0/cmake/checks/cxx11.cpp
/home/jhros/opencv-3.3.0/cmake/checks/cxx11.cpp:4:2: error: #error "C++11 is not supported"
 #error "C++11 is not supported"
  ^
/home/jhros/opencv-3.3.0/cmake/checks/cxx11.cpp: In function ‘int main()’:
/home/jhros/opencv-3.3.0/cmake/checks/cxx11.cpp:11:10: error: ‘res’ does not name a type
     auto res = test();
          ^
/home/jhros/opencv-3.3.0/cmake/checks/cxx11.cpp:12:12: error: ‘res’ was not declared in this scope
     return res;
            ^
CMakeFiles/cmTC_f915e.dir/build.make:65: recipe for target 'CMakeFiles/cmTC_f915e.dir/cxx11.cpp.o' failed
make[1]: *** [CMakeFiles/cmTC_f915e.dir/cxx11.cpp.o] Error 1
make[1]: Leaving directory '/home/jhros/opencv-3.3.0/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_f915e/fast' failed
make: *** [cmTC_f915e/fast] Error 2

And my CMakeLists (not all of it):

# ----------------------------------------------------------------------------
#  Root CMake file for OpenCV
#
#    From the off-tree build directory, invoke:
#      $ cmake <PATH_TO_OPENCV_ROOT>
#
# ----------------------------------------------------------------------------

# Disable in-source builds to prevent source tree corruption.
if(" ${CMAKE_SOURCE_DIR}" STREQUAL " ${CMAKE_BINARY_DIR}")
  message(FATAL_ERROR "
FATAL: In-source builds are not allowed.
       You should create separate directory for build files.
")
endif()


include(cmake/OpenCVMinDepVersions.cmake)

if(CMAKE_GENERATOR MATCHES Xcode AND XCODE_VERSION VERSION_GREATER 4.3)
  cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
elseif(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore)
  cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
  #Required to resolve linker error issues due to incompatibility with CMake v3.0+ policies.
  #CMake fails to find _fseeko() which leads to subsequent linker error.
  #See details here: http://www.cmake.org/Wiki/CMake/Policies
  cmake_policy(VERSION 2.8)
else()
  cmake_minimum_required(VERSION "${MIN_VER_CMAKE}" FATAL_ERROR)
endif()

# Following block can broke build in case of cross-compilng
# but CMAKE_CROSSCOMPILING variable will be set only on project(OpenCV) command
# so we will try to detect crosscompiling by presense of CMAKE_TOOLCHAIN_FILE
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
  if(NOT CMAKE_TOOLCHAIN_FILE)
    # it _must_ go before project(OpenCV) in order to work
    if(WIN32)
      set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
    else()
      set(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Installation Directory")
    endif()
  else(NOT CMAKE_TOOLCHAIN_FILE)
    #Android: set output folder to ${CMAKE_BINARY_DIR}
    set( LIBRARY_OUTPUT_PATH_ROOT ${CMAKE_BINARY_DIR} CACHE PATH "root for library output, set this to change where android libs are compiled to" )
    # any crosscompiling
    set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Installation Directory")
  endif(NOT CMAKE_TOOLCHAIN_FILE)
endif()

if(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore)
  set(WINRT TRUE)
endif(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore)

if(WINRT)
  add_definitions(-DWINRT -DNO_GETENV)

  # Making definitions available to other configurations and
  # to filter dependency restrictions at compile time.
  if(CMAKE_SYSTEM_NAME MATCHES WindowsPhone)
    set(WINRT_PHONE TRUE)
    add_definitions(-DWINRT_PHONE)
  elseif(CMAKE_SYSTEM_NAME MATCHES WindowsStore)
    set(WINRT_STORE TRUE)
    add_definitions(-DWINRT_STORE)
  endif()

  if(CMAKE_SYSTEM_VERSION MATCHES 10)
    set(WINRT_10 TRUE)
    add_definitions(-DWINRT_10)
  elseif(CMAKE_SYSTEM_VERSION MATCHES 8.1)
    set(WINRT_8_1 TRUE)
    add_definitions(-DWINRT_8_1)
  elseif(CMAKE_SYSTEM_VERSION MATCHES 8.0)
    set(WINRT_8_0 TRUE)
    add_definitions(-DWINRT_8_0)
  endif()
endif()

if(POLICY CMP0020)
  cmake_policy(SET CMP0020 OLD)
endif()

if(POLICY CMP0022)
  cmake_policy(SET CMP0022 OLD)
endif()

if(POLICY CMP0023)
  cmake_policy(SET CMP0023 NEW)
endif()

if(POLICY CMP0026)
  # silence cmake 3.0+ warnings about reading LOCATION attribute
  cmake_policy(SET CMP0026 OLD)
endif()

if(POLICY CMP0042)
  cmake_policy(SET CMP0042 NEW)
endif()

if(POLICY CMP0046)
  cmake_policy(SET CMP0046 OLD)
endif()

if(POLICY CMP0051)
  cmake_policy(SET CMP0051 NEW)
endif()

if(POLICY CMP0056)
  cmake_policy(SET CMP0056 NEW)
endif()

if(POLICY CMP0067)
  cmake_policy(SET CMP0067 NEW)
endif()

include(cmake/OpenCVUtils.cmake)

# must go before the project command
ocv_update(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE)
if(DEFINED CMAKE_BUILD_TYPE)
  set_property( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${CMAKE_CONFIGURATION_TYPES} )
endif()

enable_testing()

project(OpenCV CXX C)

if(MSVC)
  set(CMAKE_USE_RELATIVE_PATHS ON CACHE INTERNAL "" FORCE)
endif()

ocv_cmake_eval(DEBUG_PRE ONCE)

ocv_clear_vars(OpenCVModules_TARGETS)

include(cmake/OpenCVDownload.cmake)

# ----------------------------------------------------------------------------
# Break in case of popular CMake configuration mistakes
# ----------------------------------------------------------------------------
if(NOT CMAKE_SIZEOF_VOID_P GREATER 0)
  message(FATAL_ERROR "CMake fails to deterimine the bitness of target platform.
  Please check your CMake and compiler installation. If you are crosscompiling then ensure that your CMake toolchain file correctly sets the compiler details.")
endif()

# ----------------------------------------------------------------------------
# Detect compiler and target platform architecture
# ----------------------------------------------------------------------------
OCV_OPTION(ENABLE_CXX11 "Enable C++11 compilation mode" "${OPENCV_CXX11}")
include(cmake/OpenCVDetectCXXCompiler.cmake)

# Add these standard paths to the search paths for FIND_LIBRARY
# to find libraries from these locations first
if(UNIX AND NOT ANDROID)
  if(X86_64 OR CMAKE_SIZEOF_VOID_P EQUAL 8)
    if(EXISTS /lib64)
      list(APPEND CMAKE_LIBRARY_PATH /lib64)
    else()
      list(APPEND CMAKE_LIBRARY_PATH /lib)
    endif()
    if(EXISTS /usr/lib64)
      list(APPEND CMAKE_LIBRARY_PATH /usr/lib64)
    else()
      list(APPEND CMAKE_LIBRARY_PATH /usr/lib)
    endif()
  elseif(X86 OR CMAKE_SIZEOF_VOID_P EQUAL 4)
    if(EXISTS /lib32)
      list(APPEND CMAKE_LIBRARY_PATH /lib32)
    else()
      list(APPEND CMAKE_LIBRARY_PATH /lib)
    endif()
    if(EXISTS /usr/lib32)
      list(APPEND CMAKE_LIBRARY_PATH /usr/lib32)
    else()
      list(APPEND CMAKE_LIBRARY_PATH /usr/lib)
    endif()
  endif()
endif()

# Add these standard paths to the search paths for FIND_PATH
# to find include files from these locations first
if(MINGW)
  if(EXISTS /mingw)
      list(APPEND CMAKE_INCLUDE_PATH /mingw)
  endif()
  if(EXISTS /mingw32)
      list(APPEND CMAKE_INCLUDE_PATH /mingw32)
  endif()
  if(EXISTS /mingw64)
      list(APPEND CMAKE_INCLUDE_PATH /mingw64)
  endif()
endif()

EDIT

CMake version -3.5

Compiler GNU 5.4.1

EDIT2

I'm compiling with GNU 7.1.0 now but I get different error

Build output check failed:
    Regex: 'command line option .* is valid for .* but not for C\+\+'
    Output line: 'cc1plus: warning: command line option ‘-Wmissing-prototypes’ is valid for C/ObjC but not for C++'
Compilation failed:
    source file: '/home/jhros/opencv-3.3.0/build/CMakeFiles/CMakeTmp/src.cxx'
    check option: ' -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations  -Wmissing-prototypes'
Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
  • Did you make some changes to the original OpenCV `CMakeLists.txt`? – Dan Mašek Aug 05 '17 at 12:54
  • 1
    @DanMašek yes I turned ON some components in OCV_OPTION that I'll be using later – Ja_cpp Aug 05 '17 at 12:56
  • OK, could you, (instead of pasting this fairly long chunk of an even longer file) either summarize the changes somehow, or perhaps provide a diff? | What if you add `-DENABLE_CXX11=OFF` to you cmake command line? – Dan Mašek Aug 05 '17 at 13:05
  • 1
    I tried with that flag but the error is the same.. I just edited my post – Ja_cpp Aug 05 '17 at 13:27
  • Ya, few more questions -- what exact version of CMake? Which compiler is this (gcc?) and which version? Could you perhaps try with a newer version of gcc or maybe clang? (Oh and just to be sure, clean the build directory before you run CMake, so there's nothing left over that could possibly interfere) – Dan Mašek Aug 05 '17 at 13:38
  • 1
    @DanMašek I delete build folder every time I re compile (edited my post) – Ja_cpp Aug 05 '17 at 13:48
  • Why are you on GNU CC v5? Version 7 is available. – Mark Setchell Aug 05 '17 at 21:52
  • @MarkSetchell Do you think it could be the issue? I'll try GCC 7 – Ja_cpp Aug 06 '17 at 12:01
  • 2
    @DanMašek adding `-DENABLE_CXX11=ON` solved the same problem for me – Nimrod Morag Aug 27 '17 at 13:53

5 Answers5

19

I finally compile OpenCV 3.3 from source successfully, however I still don't know why it wouldn't compile before. All what I had modified is the flag OPENCV_EXTRA_MODULES_PATH to the reference where the source files of the modules of opencv_contrib 3.3.0 are, like so:

$ cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/jhros/opencv-3.3.0/opencv_contrib-3.3.0/modules  \
-D BUILD_SHARED_LIBS=ON \
-D WITH_GTK=ON \    
-D BUILD_EXAMPLES=ON ..  

I got an infinite number of warnings of kind (and many others):

warning: dynamic exception specifications are deprecated in C++11

And also you might get an error with newer version of GCC which is not supported by cuda:

#error -- unsupported GNU version! gcc versions later than 5 are not supported!

But this could be solved by setting cmake to use older version of GCC while compiling OpenCV

CMAKE_C_COMPILER=/usr/bin/gcc-5
Rishabh Agrahari
  • 3,447
  • 2
  • 21
  • 22
Ja_cpp
  • 2,426
  • 7
  • 27
  • 49
  • 5
    This solution worked for me. I had a typo in the value of `OPENCV_EXTRA_MODULES_PATH`. Fixing it to the proper value solved the C++11 error. – exhuma Aug 13 '17 at 14:13
2

I installed OpenCV 3.3.1 on Linux Mint 18.2, followed this article (https://www.learnopencv.com/install-opencv3-on-ubuntu/) and I had the same problem. I changed cmake command from

cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
  -D INSTALL_C_EXAMPLES=ON \
  -D INSTALL_PYTHON_EXAMPLES=ON \
  -D WITH_TBB=ON \
  -D WITH_V4L=ON \
  -D WITH_QT=ON \
  -D WITH_OPENGL=ON \
  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
  -D BUILD_EXAMPLES=ON ..

to

cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -DCMAKE_INSTALL_PREFIX=/usr/local \
  -D INSTALL_C_EXAMPLES=OFF \
  -D INSTALL_PYTHON_EXAMPLES=OFF \
  -D WITH_TBB=ON \
  -D WITH_V4L=ON \
  -D WITH_QT=ON \
  -D WITH_OPENGL=ON \
  -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
  -D BUILD_EXAMPLES=OFF ..

(changed from -D CMAKE_INSTALL_PREFIX=/usr/local \ to -DCMAKE_INSTALL_PREFIX=/usr/local \) and it worked for me. Hope useful. Note: 3.3.0 is not work. (Sorry about my English)

T. Hieu
  • 21
  • 2
2

i also had this error, and add argument OPENCV_EXTRA_MODULES_PATH point to the opencv_contrib solved it

ayy
  • 139
  • 2
  • 9
1

The problem here is not that you are trying to use C++11 but exactly the opposit, the source code depends on it (judging by the error given for auto res = test(); which is a C++11 feature).

For GCC 5.4 the default C++ standard is C++98 with GNU extensions, as stated in the documentation, although you can explicitly set the C++11 standard (which is almost fully supported in GCC 5.4) using the -std=c++11 flag. This would probably solve the issue however updating to GCC 7.1 (as Mark Setchell advised) is much better idea and solves the issue out of the box (the default standard in 7.1 is C++14 with GNU extensions).

As for how to pass the flag: you only need to add set(CMAKE_CXX_FLAGS "-std=c++11") to the beginning of the CMakeLists.txt or add a string entry in CMake GUI where the name is CMAKE_CXX_FLAGS and the value is -std=c++11.

user2986898
  • 413
  • 3
  • 10
  • I have encountered the same problem as @Ja_cpp. Unfortunately, this solution did not work for me. – Alex Bailo Aug 08 '17 at 03:27
  • Now I'm compiling with GNU 7.1.0, I get different error!! – Ja_cpp Aug 08 '17 at 08:13
  • 1
    I cannot comment your answer so I'm gonna go ahead and write below mine. `-Wmissing-prototypes` is not a valid C++ flag (it is for C though) so I absolutely have no idea why it was used, maybe CMake didn't find the correct compiler. As for the warnings; they are expeced during compilation of OpenCV (at least they are in my experience) however those that are related to C++11 _might_ go away using the `-DENABLE_CXX11=ON` flag. – user2986898 Aug 09 '17 at 20:29
  • @user2986898 When I upgraded my compiler to the new version, I somehow was compiling with gcc only and in fact it needs g++ also.. That's why I got that error. I mentioned that so as people who will get the same error will find a solution. Thanks for pointing that out – Ja_cpp Aug 14 '17 at 19:51
1

adding -DENABLE_CXX11=ON solve it for me

Or Hirshfeld
  • 106
  • 2
  • 11