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:
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.
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()
#####################################
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
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".
Other details: -Compiling on a raspberry pi 3 running debian jessie -CXX compiler is GNU 4.9.2 -CMAKE 3.0.2
I always enable c++11 in my code using CMake this way:
My compiler is gcc (Debian 4.9.2-10) 4.9.2, however in my workplace I also use other versions and this approach always works.
EDIT (to avoid variable overwriting):
Since the current cmake release is 3.10, I thought it may be appropriate to identify the newer method. While the suggestion to use add_compiler_
For anyone looking here at a more modern version of cmake (3.1+), The most appropriate answer is not to identify a version of a given compiler but to let CMAKE know what features need to be available.
CMake add support for
CXX_STANDARD
andCXX_STANDARD_REQUIRED
properties on 3.1 version.CXX_STANDARD
: Take one ofCMAKE_CXX_STANDARD
values and they are98, 11 and 14
. If you passCXX_STANDARD 11
and you compiler do not support c++11CXX_STANDARD
become 98 automatically and cmake do not give you any error ifCXX_STANDARD_REQUIRED
isOFF
or unset. If your setCXX_STANDARD_REQUIRED "ON" CXX_STANDARD
specific value become as a required property to build and cmake handle this.In orde to use
CHECK_CXX_COMPILER_FLAG
you nedd to include CheckCXXCompilerFlag module:If you have a old cmake you need handle complicate and not portable flags from compilers sush as:
In CMake versions earlier than 3.1, we use
to add compile options to the compiler call as described in the CMake Docs.
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.
Edit: For the sake of completeness: In CMake version 3.1 and newer, if you want to force C++11, you need the following lines:
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:Edit: But beware that C++11 support in GCC 4 is not complete and there might be things that behave differently from the defined standard.