The LLVM C++ compiler has full support for C++11 standard. Is there a way to set C++11 as the default standard without adding -std=c++11 compiler flag every time? I tried setting CCXFLAGS environment variable to -std=c++11, but with no luck.
Use clang 6.0.0 or higher. The default C++ dialect is now C++14.
http://releases.llvm.org/6.0.1/tools/clang/docs/ReleaseNotes.html#c-language-changes-in-clang
Related
I am trying to build cmake source, which requires C++11.
The build halts and apparently the complaint is that C++11 is not detected. The g++ mode is actually set to -std=gnu++17
This is part of the console log
---------------------------------------------
CMake 3.18.20200919, Copyright 2000-2020 Kitware, Inc. and Contributors
Found GNU toolchain
C compiler on this system is: gcc
C++ compiler on this system is: g++ -std=gnu++17
Makefile processor on this system is: make
g++ has setenv
g++ has unsetenv
g++ does not have environ in stdlib.h
g++ has stl wstring
g++ has <ext/stdio_filebuf.h>
---------------------------------------------
g++ -std=gnu++17 -DCMAKE_BOOTSTRAP -DCMake_HAVE_CXX_MAKE_UNIQUE=1 -c $HOME/Apps/CMake-master/Source/cmAddCustomCommandCommand.cxx -o cmAddCustomCommandCommand.o
This is part of the error in the log file...
In file included from /usr/include/c++/5/unordered_map:35:0,
from cmake_bootstrap_11920_test.cxx:4:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
^
cmake_bootstrap_11920_test.cxx:7:2: error: #error "Compiler is not in a mode aware of C++11."
#error "Compiler is not in a mode aware of C++11."
^
cmake_bootstrap_11920_test.cxx:70:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
int Member = 1;
Looking around on the web, I noticed that C++11 is only available after gcc version 4.6.
I checked my version, and it seems to be above.
g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
I understand the -std=c++11 flag is used to enable the C++11 features in g++, but I don't seem to know what I am doing in this case.
I tried editing the CompileFlags.cmake file, but no change occurs.
I came upon this page which points to the cmake source I am using.
It says...
bootstrap: Require compiler mode aware of C++11
Some compilers have enough features enabled in their default modes to
pass our simple C++11 unique_ptr check but do not enable enough to build
CMake. Poison this case so that we choose one of the explicit `-std=`
options for such compilers.
Not sure what that means exactly.
How exactly do I change the g++ mode, to C++11, so that on running the bootstrap command, C++11 is used?
Or, in other words, how do I change std to point to C++11 (-std=c++11)?
First of all, you have g++ version 5.4.0 in your host PC installed, which is good, cause it means this is also supports the C++11, which you want to use.
To set it up, you could define it in your CMakeList.txt file:
set (CMAKE_CXX_STANDARD 11)
that should do the trick.
Please also check the documentation:
https://cmake.org/cmake/help/v3.1/variable/CMAKE_CXX_STANDARD.html
Usually, I would suggest to use the latest standard that you compiler is supporting (https://gcc.gnu.org/projects/cxx-status.html), cause you'll get also the latest features introduced in that standard. Exception for this rather in case you are working with legacy codes.
I'm trying to run C++11 code in CLion but it doesn't work. It says:
...
/projects/CLion/untitled/main.cpp:7:1: note: C++11 ‘constexpr’ only available with -std=c++11 or -std=gnu++11
...
I tried to set CMAKE_C_FLAGS to -std=c++11 or -std=gnu++11 but I still have the same problem. Regular C++ code compiles fine.
What flag do I have to set in CLion's CMake window to compile my C++11 code?
I tried to set CMAKE_C_FLAGS
According to the documentation the CMAKE_C_FLAGS set C language flags for all build types. For C++ you need use CMAKE_CXX_FLAGS instead:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
For CMake 3.1 or later, you can set the CMAKE_CXX_STANDARD variable to 11:
Default value for CXX_STANDARD property of targets.
This variable is used to initialize the CXX_STANDARD property on all targets.
CXX_STANDARD documentation:
The C++ standard whose features are requested to build this target.
This property specifies the C++ standard whose features are requested to build this target. For some compilers, this results in adding a flag such as -std=gnu++11 to the compile line.
Supported values are 98, 11 and 14.
If the value requested does not result in a compile flag being added for the compiler in use, a previous standard flag will be added instead. This means that using:
set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
with a compiler which does not support -std=gnu++11 or an equivalent flag will not result in an error or warning, but will instead add the -std=gnu++98 flag if supported. This “decay” behavior may be controlled with the CXX_STANDARD_REQUIRED target property.
See the cmake-compile-features(7) manual for information on compile features.
This property is initialized by the value of the CMAKE_CXX_STANDARD variable if it is set when a target is created.
I'm running Ubuntu 15.10 with CUDA 7.5. CMmake is v3.2.2, NVCC is release 7.5, v7.5.17; GCC is Ubuntu 5.2.1-22ubuntu2 v5.2.1
Triggering C++11 in regular projects is easy with:
project(foo CXX)
set(TARGET foo CMAKE_CXX_STANDARD 11)
I'm defining my CUDA project with:
find_package(CUDA REQUIRED)
CUDA_ADD_EXECUTABLE(foo ${foo_src} ${foo_hdr} ${foo_cu})
But the C++11 support doesn't get propagated to NVCC. I have to add:
list(APPEND CUDA_NVCC_FLAGS "-std=c++11")
This seems like a kludge. There was evidently work on this recently according to this task, but I haven't been able to find the results.
How do I get CMake to automatically set the C++11 flags when declaring the project as C++11?
EDIT: I've retuned to to this question with CUDA 8.0 and CMake 3.5.1.
From the documentation, set(CUDA_PROPAGATE_HOST_FLAGS ON) will propagate the contents of CMAKE_CXX_FLAGS, so the following triggers C++11 for both cpp and nvcc:
set (CMAKE_CXX_FLAGS "--std=c++11")
set (CUDA_PROPAGATE_HOST_FLAGS ON)
However, set(CMAKE_CXX_STANDARD 11) does not impact CMAKE_CXX_FLAGS, so the following gives compiler errors for C++11 device code, as there's nothing to propagate:
set (CMAKE_CXX_STANDARD 11)
set (CUDA_PROPAGATE_HOST_FLAGS ON)
I can't seem to find a combination of CMake commands that avoids explicitly setting --std=c++11 in either CXX or CUDA flags.
Since CMake 3.8 (since CMake supports CUDA as a language) there is a new target property CUDA_STANDARD. Although its name is quite confusing, it adds the -std=XXX to the nvcc compile command.
With a recent CMake version the proper way would be
cmake_minimum_required(VERSION 3.8.2)
enable_language(CUDA)
add_executable(foo ${foo_src} ${foo_cu})
set_property(TARGET foo PROPERTY CUDA_STANDARD 11)
I can't even get the basic codecvt example from cppreference.com to compile on GCC 4.9 or Clang 3.4, e.g.:
http://goo.gl/HZ5GLH
http://coliru.stacked-crooked.com/a/345d6d89949ac1e6
According to libstdc++ manual, part 22.4.1, it is missing the support for codecvt, even on the latest version.
And libc++ has complete support for C++11 and C++14 features, so you should use it on CLang, specifying the -stdlib=libc++ compiler flag (make sure you have it installed).
Edit: As of current versions of libstdc++, codecvt is now supported.
I'm trying to use the function stoi() in a program which is a C++11 function but I can't configure my compiler to use C++11. I added the (-std=c++11) compiler flag but it didn't work. I also tried the (-std=c++0x) and it also didn't work. I also tried these options after updating the compiler to GCC 4.8 that should support C++11 and didn't work. I don't know what to do.