使用CMake编译OpenCV项目时出错
Error compiling an OpenCV project with CMake
我遵循本教程,试图创建一些OpenCV项目。它在Windows和Visual Studio中运行得很好,但后来我尝试使用以下CmakeLists.txt在我的Ubunto VM中运行它:
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} )
当我运行cmake .
时,似乎一切都很好:
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
但是当我运行make
时,我会得到以下错误:
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
我对CMake很陌生,但我很确定问题出在我使用多个.cpp文件和使用CMake的方式上。原因是当我尝试在教程中运行预览步骤时,当项目只包括一个.cpp文件时,一切都很好
您可以在此处看到确实有效的源代码(进行了一些小的更改,如删除#include <opencvhighgui.h> #include <opencvcv.h>
并改为编写:#include <opencv2/opencv.hpp>
。而的源代码在这里也不起作用,只是做了同样的小改动。此外,该项目还包括视频中描述的非常简单的Fruit.cpp和Fruit.h。
我浏览了不太友好的CMake官方教程,以及更友好的johnlamp和OpenCV教程,但在这里找不到我做错了什么。
错误告诉您需要为编译器启用C++11功能。您可以通过设置编译器标志-std=c++11
(对于旧版编译器则为-std=c++0x
)来完成此操作。在CMake中,可以在CMAKE_C_FLAGS
/CMAKE_CXX_FLAGS
变量中定义编译器标志,具体取决于目标语言。
在您的情况下:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
相关文章:
- 当我尝试通过 mingw 使用 CMake 和 SFML 库编译项目时出错
- 如何使用我构建的库,而不会从源代码出错,但不为我自己的项目编译?
- Flex/Bison:构建示例项目以生成 CPP 时出错
- 将项目导入 Android Studio 后出错
- 将 Dr. Memory 与 Visual Studio 项目配合使用时出错:缺少应用程序所需的库
- 使用 C++Builder 项目读取字符串中的文件时出错
- 在 Visual Studio 2015 中生成 vsal 项目时出错
- 创建新的 Visual Studio c++ 项目时出错
- 编译包含 CPLEX 工具的项目时出错
- CMake 和/或在为项目添加提升时出错
- 链接 gtest 项目时出错
- iOS:在真实设备上运行项目时出错
- 在qmake项目中编译sqlite时出错
- LNK1104将Boost库添加到Qt项目时出错
- 仅在从命令行构建Qt项目时出错
- 运行调试库项目时发布库出错
- Qt项目qmake在构建上出错
- 将VC++项目从VS2010升级到VS2012时出错
- 在c++项目中包含dlib库时出错
- 使用CMake编译OpenCV项目时出错