【问题标题】:Compiling OpenCV 3.3 : C++11 is not supported编译 OpenCV 3.3:不支持 C++11
【发布时间】:2018-01-13 06:35:18
【问题描述】:

我正在尝试编译 OpenCV 3.3(2017 年 8 月 3 日发布)的新版本,但我收到了 C++11 的错误

这是我的cmake 命令行:

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 ..

我已经通过打开某些组件来编辑 CMakeLists(即使没有进行任何更改,错误仍然是一样的):

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

这里是我的CMakeError.log 文件:

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

还有我的 CMakeLists(不是全部):

# ----------------------------------------------------------------------------
#  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()

编辑

CMake 版本-3.5

编译器 GNU 5.4.1

EDIT2

我现在正在使用 GNU 7.1.0 进行编译,但出现不同的错误

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'

【问题讨论】:

  • 你对原来的OpenCV CMakeLists.txt做了一些改动吗?
  • @DanMašek 是的,我在 OCV_OPTION 中打开了一些稍后将使用的组件
  • 我尝试使用该标志,但错误是一样的。我刚刚编辑了我的帖子
  • @DanMašek 我每次重新编译时都会删除构建文件夹(编辑了我的帖子)
  • @DanMašek 添加-DENABLE_CXX11=ON 为我解决了同样的问题

标签: opencv c++11 compilation cmake ubuntu-16.04


【解决方案1】:

我终于成功地从源代码编译OpenCV 3.3,但是我仍然不知道为什么它以前不会编译。我所修改的只是OPENCV_EXTRA_MODULES_PATH 的标志,指向opencv_contrib 3.3.0 模块的源文件所在的引用,如下所示:

$ 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 ..  

我收到了无数种警告(以及许多其他警告):

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

而且您可能会收到 cuda 不支持的 GCC 较新版本的错误:

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

但这可以通过在编译OpenCV时将cmake设置为使用旧版本的GCC来解决

CMAKE_C_COMPILER=/usr/bin/gcc-5

【讨论】:

  • 这个解决方案对我有用。我在OPENCV_EXTRA_MODULES_PATH 的值中有错字。将其修复为正确的值解决了 C++11 错误。
【解决方案2】:

我在 Linux Mint 18.2 上安装了 OpenCV 3.3.1,按照这篇文章 (https://www.learnopencv.com/install-opencv3-on-ubuntu/),我遇到了同样的问题。我从

更改了 cmake 命令
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 ..

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 ..

(从 -D CMAKE_INSTALL_PREFIX=/usr/local \ 更改为 -DCMAKE_INSTALL_PREFIX=/usr/local \)它对我有用。希望有用。 注意:3.3.0 不起作用。 (对不起我的英语)

【讨论】:

  • 看来这里的重大变化是您关闭了所有示例。
【解决方案3】:

我也有这个错误,添加参数OPENCV_EXTRA_MODULES_PATH指向opencv_contrib解决了它

【讨论】:

    【解决方案4】:

    这里的问题不在于您尝试使用 C++11,而恰恰相反,源代码依赖于它(根据 C++11 特性auto res = test(); 给出的错误判断)。

    对于 GCC 5.4,默认 C++ 标准是带有 GNU 扩展的 C++98,如 documentation 中所述,尽管您可以使用-std=c++11 标志。这可能可以解决问题,但是更新到 GCC 7.1(正如 Mark Setchell 建议的那样)是更好的主意,并且开箱即用地解决了问题(7.1 中的默认标准是带有 GNU 扩展的 C++14 )。

    至于如何传递flag:只需要在CMakeLists.txt的开头加上set(CMAKE_CXX_FLAGS "-std=c++11")或者在CMake GUI中添加一个字符串入口,名称为CMAKE_CXX_FLAGS,值为-std=c++11

    【讨论】:

    • 我遇到了和@Ja_cpp一样的问题。不幸的是,这个解决方案对我不起作用。
    • 现在我用 GNU 7.1.0 编译,我得到了不同的错误!!
    • 我无法评论你的答案,所以我会继续写在我的下面。 -Wmissing-prototypes 不是一个有效的 C++ 标志(虽然它是用于 C 的)所以我完全不知道为什么要使用它,也许 CMake 没有找到正确的编译器。至于警告;它们在 OpenCV 编译期间被预期(至少它们是我的经验)但是与 C++11 相关的那些可能使用-DENABLE_CXX11=ON 标志消失。
    • @user2986898 当我将编译器升级到新版本时,不知何故我只用 gcc 编译,实际上它也需要 g++。这就是我得到这个错误的原因。我提到过,这样会遇到同样错误的人会找到解决方案。感谢您指出这一点
    【解决方案5】:

    添加 -DENABLE_CXX11=ON 帮我解决

    【讨论】:

      猜你喜欢
      • 2017-12-15
      • 2017-06-22
      • 2018-10-01
      • 1970-01-01
      • 2010-12-01
      • 2018-02-05
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      相关资源
      最近更新 更多