gpt4 book ai didi

c++ - 使用 CMake 编译 OpenCV 项目时出错

转载 作者:搜寻专家 更新时间:2023-10-31 01:01:18 24 4
gpt4 key购买 nike

我关注了this tutorial试图创建一些 OpenCV 项目。它在 Windows 和 Visual Studio 中运行良好,但后来我尝试使用以下 CmakeLists.txt 在我的 Ubunto VM 中使用 CMake 运行它:

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 的方式上。原因是当我尝试运行 previews step in the tutorial 时,当项目只包含一个 .cpp 文件时,一切都很好。

您可以看到有效的源代码here (有一些小的改动,比如删除 #include <opencv\highgui.h> #include <opencv\cv.h> 并改为: #include <opencv2/opencv.hpp> 。以及无效的源代码here具有相同的微小变化。此外,该项目还包括非常简单的 Fruit.cpp 和 Fruit.h,如视频中所述。

我检查了不太友好的official tutorial CMake 和更友好的 johnlampOpenCV教程,但找不到我在这里做错了什么。

最佳答案

该错误告诉您需要为编译器启用 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")

关于c++ - 使用 CMake 编译 OpenCV 项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29409681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com