cmake无法编译为C ++ 11标准 [英] cmake will not compile to C++ 11 standard
问题描述
我是C ++的新手,一直在努力进行编译/制作/链接/构建/其他操作,让我们看看是否有人可以帮助我。我进行了一些搜索,发现其他人也遇到类似的问题,但是我没有运气尝试过他们的解决方案,所以去了:
I'm new to C++ and have been struggling with compiling/making/linking/building/whatever, lets see if somebody can help me out. I did some searches and found other people with similar problems but I tried their solutions with no luck, so here goes:
使用C ++ 11功能的简单c ++程序,例如统一初始化,线程, to_string
等...生成的错误是 xxx未在作用域中声明。现在特别要使用 to_string
,并在 std
命名空间或 std :: to_string
创建错误 to_string不是STD成员。所以很明显它不是用C ++ 11编译的。
A simple c++ program that uses C++ 11 functionality such as uniform initialization, threads, to_string
, etc... generates errors that "xxx" was not declared in the scope. Specifically right now I'd like to use to_string
, and using it in the std
namespace or specifically std::to_string
creates the error "to_string" is not a member of STD. So clearly it's not compiling with C++ 11.
所以这是我的make文件:
So here's my make file:
#####################################
cmake_minimum_required (VERSION 2.8)
project (raspicam_test)
find_package(raspicam REQUIRED)
find_package(OpenCV)
IF ( OpenCV_FOUND AND raspicam_CV_FOUND)
MESSAGE(STATUS "COMPILING OPENCV TESTS")
add_executable (main main.cpp)
#target_compile_features(main PRIVATE cxx_range_for)
set_property(TARGET main PROPERTY CXX_STANDARD 11)
target_link_libraries (main ${raspicam_CV_LIBS})
ELSE()
MESSAGE(FATAL_ERROR "OPENCV NOT FOUND IN YOUR SYSTEM")
ENDIF()
#####################################
当您可以看到我在树莓派上玩OpenCV。但是,如果没有C ++ 11函数,该程序就可以编译并运行。但是我想从C ++ 11添加线程和其他东西。我根据CMAKE文档添加了 set_property(TARGET main PROPERTY CXX_STANDARD_REQUIRED 11)
行:
As you can see I'm playing around with OpenCV on a raspberry pi. But without the C++11 functions the program compiles and runs no issues. But I would like to add threads and other goodies from C++11. I added the line set_property(TARGET main PROPERTY CXX_STANDARD_REQUIRED 11)
according to the CMAKE documentation:
https://cmake.org/cmake/help/v3.1/prop_tgt/CXX_STANDARD.html
并且生成的错误没有区别。我首先没有 _REQUIRED
做到了,然后有了它。我还尝试了 target_compile_features()
,但是CMAKE返回了未知的CMAKE命令。
And it made no difference in the errors generated. I did it first without the _REQUIRED
and then with it. I also tried target_compile_features()
instead but CMAKE returned with "unknown CMAKE command".
其他详细信息:
-在运行debian jessie的raspberry pi 3上编译
-CXX编译器是GNU 4.9.2
-CMAKE 3.0.2
Other details: -Compiling on a raspberry pi 3 running debian jessie -CXX compiler is GNU 4.9.2 -CMAKE 3.0.2
推荐答案
在<3.1> 之前的CMake版本中,我们使用
In CMake versions earlier than 3.1, we use
add_compile_options(-std=c++11) # CMake 2.8.12 or newer
将编译选项添加到 CMake文档中所述。
to add compile options to the compiler call as described in the CMake Docs.
这可能不像Alvaro的回答那样可移植,但是它更具可读性,而且由于您在使用RasPi,我想GCC和Clang就像目标编译器一样。
That's propably not as portable as the one in Alvaro's answer, but it's more readable and since you are on you RasPi, I guess, GCC and Clang as target compilers will do.
编辑:为了完整起见:在CMake版本 3.1和更高版本中,如果要强制使用C ++ 11,则需要执行以下操作:行:
For the sake of completeness: In CMake version 3.1 and newer, if you want to force C++11, you need the following lines:
set(CMAKE_CXX_STANDARD 11) # C++11...
set(CMAKE_CXX_STANDARD_REQUIRED ON) #...is required...
set(CMAKE_CXX_EXTENSIONS OFF) #...without compiler extensions like gnu++11
这将启用编译期间所有目标的选项。如果您想更细致地控制它,请参阅Alvaro的答案或 set_taget_properties()
的CMake文档,然后看起来像这样:
This enables the options for all targets during compilation. If you want to control this more fine-grained, see Alvaro's answer or the CMake Docs of set_taget_properties()
, which then looks something like this:
set_target_properties(myTarget PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS OFF
)
编辑:但是请注意,GCC 4中的C ++ 11支持并不完整,可能行为与定义的标准不同。
But beware that C++11 support in GCC 4 is not complete and there might be things that behave differently from the defined standard.
这篇关于cmake无法编译为C ++ 11标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!