I am using Eclipse CDT 3.8.1 with GCC Cross Compiler 8.6.0. I know how to change the C++ standard in the project properties, but I don't know what standard is used by default.
I know the default is not C++11 and the Eclipse language support page doesn't mention C++03, so I suspect the default is C++98. However, Eclipse CDT must support C++03 because adding the compiler flag -std=c++03 doesn't cause any errors.
https://www.eclipse.org/community/eclipse_newsletter/2017/april/article3.php
What is the default standard when I create a new project?
Eclipse's parser itself doesn't have a notion of a C++ standard mode. It will recognize all the C++ features that have been implemented in its parser (which, as of writing this, is all C++98 and C++11 features, some (but not all) C++14 features, and a handful of C++17 features).
However, standard library headers often contain sections that are conditional on macros denoting the C++ standard version (e.g. #if __cplusplus >= 201103 is a common check for "C++11 or later"). To determine the value of these macros, Eclipse invokes the compiler specified in the project's toolchain to discover built-in macros. The discovered value of e.g. the __cplusplus macro will depend on what standards mode the compiler runs in for this invocation.
The flags to this compiler invocation are specified in the project properties, as you mentioned. If you don't provide a flag there, the compiler will use whatever its default mode is. I believe GCC has been using -std=c++14 as the default from GCC 6 onwards. (Though I don't quite know what "GCC Cross Compiler 8.6.0" is. According to the GCC website, the latest version is 8.1.)
Related
I have a c++14 project that currently targets gcc 7.2, and I am looking to backport code from a project that targets c++17. This project makes extensive use of if constexpr. gcc 7.2 supports if constexpr with the --std=c++1z flag, however it brings along all the other (at the time) experimental C++17 features.
Is there a way to enable only specific language features, in this case if constexpr, in gcc 7.2?
No it is not possible. It's all or nothing.
There is some limited level of control over language dialect in g++
https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html
If these dialects are used it can raise a warning and you can turn this warning into an error.
Another way could be to create plugins for clang-tidy or clang-query to check your C++ code base do not use any construct you don't want, but it becomes a rather large work to achieve the intended purpose.
I saw many threads regarding how to enable certain C++ standard e.g.
add_definitions("-std=c++14")
and
set (CMAKE_CXX_STANDARD 11)
But how to find which standard by default g++ compiler following while compiling code ?
add_definitions("-std=c++14") is not the correct way to enable C++14. The right way either the second one you mentioned: set(CMAKE_CXX_STANDARD 14), or set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14").
add_definitions is for macro definitions, equivalent to #define in your source code.
About your question, it's bad practice to depend on the default version of C++ in your compiler. Even Visual Studio now defines the standard you want to use, which is a new thing in VS. Standards are made to solve this problem by having them specified. So, in your project, you define the target standard, and you define it in your make file. You should not expect your code to work on all C++ standards.
If you're developing an application, then you should not adjust your code based on available compiler version, instead you should specify which compiler standard your code requires:
set (CMAKE_CXX_STANDARD 11)
If you're building a code library that is meant to be used in different contexts, use compiler feature detection and conditional compilation. For example:
write_compiler_detection_header(
FILE "${CMAKE_CURRENT_BINARY_DIR}/compiler_features.h"
PREFIX Foo
COMPILERS GNU
FEATURES
cxx_variadic_templates
)
And then:
#include "compiler_features.h"
#if Foo_COMPILER_CXX_VARIADIC_TEMPLATES
# include "with_variadics/interface.h"
#else
# include "no_variadics/interface.h"
#endif
This is especially useful with compilers like MSVC which have many features from C++17 while also lacking some basic features from C++11 (e.g. expression SFINAE).
CMake has built-in detectors for many popular C++ features.
PS. If you're really curious, G++ up to 5.0 uses by default gnu++98 standard, and from 6.0 gnu++14 (GNU dialect of -std=c++14).
I'm working with eclipse and I can choose the language standard and/or other dialect flags. When I want to work with the latest language standard, do I need to declare as shown in the image or leave the language standard blank and just define c++11, or the other way around, just define the language standard c++1y and leave the flags empty?
According to Options Controlling C Dialect -std=c++1y is just a deprecated alias to -std=c++14, not to -std=c++11. Even though Eclipse offered a proper alternative option -std=c++0x, it is also deprecated, so you better leave language standard blank and set -std=c++11 among flags.
I'm currently using gcc 4.8.2 with no std option to compile my c/c++ code.
Now I would like to use some of the new C/C++ features that are provided by the newer language versions of c/c++.
(Un?)Fortunately gcc understands many flavors of C/C++:
c90, c99, c11, gnu90, gnu99, gnu11
c++98, c++03, c++11, gnu++98, gnu++03, gnu++11
Currently I'm asking myself:
Do I need for every c/c++ version a separate library or is it possible to use one library for multiple C/C++ versions?
Especially can I link a libary compiled with a specific c/c++ version, when I use the corresponding c/c++-version with the gnu extensions?
Clarification (based on the comments)
Please note, that I'm using just one compiler. Not two gcc's that only differ in the revision number.
I'm only asking for ABI incompatibilities between the different std-options, when using only one gcc compiler.
In general:
No you can't combine different language versions in the same program, this will cause "One Definition Rule" violations in many library headers.
You may find in limited cases that a few classes actually don't change with the language version. However this is rare, rvalue references are required by the C++11 Standard, and not available in C++03 mode at all.
As for versions with and without support for GNU extensions, you're likely to have more success, but you will still need to run each header through the preprocessor and verify that the exact same sequence of tokens is seen by the compiler using both options.
And that's completely apart from ABI changes, that could cause memory layout or name mangling to differ between compiler variants.
You can also avoid one-definition rule violations on your own public APIs by avoiding using any version and language-specific features. Essentially, this means a flat C API.
I created a standard c++ library project and I tried to include <tuple> but it doesn't seem to be included as a top level header, instead I still need to include <tr1/tuple>. The llvm compiler dialect is set to -std=c++11.
Am I correct in assuming that even though xcode 4.4 has a c++11 compliant compiler it doesn't ship with a C++11 compliant standard library yet ?
This question might be related to this one.
The library selection configuration settings is independent of the compiler dialect, and is not found under the "basic" view of the project configuration (unless you edit it). Specifically the setting is called "C++ Standard Library" and you can find it in the "all" view.
I suspect you were expecting this to appear with (or be part of) the compiler dialect setting.