Compiling C++11 With G++

Compiling C++11 with g++

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable.

Assuming you are invoking g++ from the command line (terminal):

$ g++ -std=c++11 your_file.cpp -o your_program

or

$ g++ -std=c++0x your_file.cpp -o your_program

if the above doesn't work.

How to force g++ to compile c++11 standard only?

This is related to libstdc++ that ships with gcc. In libstdc++, bits/vector.tcc, you have the std::vector<bool> specialization with

#if __cplusplus >= 201103L
template<typename _Tp, typename _Alloc>
template<typename... _Args>
// ... more stuff
vector<_Tp, _Alloc>::emplace_back(_Args&&... __args)

The preprocessor branch enables the member function for C++11. In libc++ (the standard library implementation within the Llvm project), it's different:

#if _LIBCPP_STD_VER > 11
template <class... _Args>
#if _LIBCPP_STD_VER > 14
_LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args)
#else
_LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args)
#endif

so here, emplace_back is only defined with a standard beyond C++11. You cannot do much about it, but I see two options.

  1. To achieve strict conformance to C++11, not only compile with g++, but also with clang++. clang uses libc++ by default on MacOS, and on Linux you can pass some flag to enforce that (otherwise, it uses libstdc++), which does complain about emplace_back with -std=c++11. Then use the results of compiling with clang to adjust your sources that are otherwise built with gcc.

  2. Compile with gcc, but tell the compiler to use libcxx. From here, this also complains:

    g++ -std=c++11 -nostdinc++ -I /path/to/libcxx/include -nodefaultlibs \
    -lc++ -lc++abi -lm -lc -lgcc vecbool-emplace_back.cpp

I would go with option 1, because it hardens your program in different ways, too, not only standard compliance. Also, option 2 is cumbersome.

how to define -std=c++11 as default in g++

Yes, you typically set this in a Makefile:

CXXFLAGS=-std=c++11 

One layer above you can also detect a suitable compiler via autoconf, cmake or whichever other meta-buildtool you might deploy.

You of course play games as define g++11 as g++ -std=c++11 but such set-ups are not portable.

g++-6.* will default to c++14 so at some this switch will be implicit. But it might take a really long time for all those RHEL and CentOS boxen with g++-4.4.* to disappear. Those may not even handle your current project...

compiling c++ with g++ from -c to -std=c++11

-c means run only the compiler and not the linker. You should keep it when adding the other flag if your intention is only to change the c++ version.

Compiling C++11 with g++ 5.4

This code below reproduces the original error:

class CFrameProd{
public:
CFrameProd(){
MilGrabBufferList_ = {0};
}
private:
long MilGrabBufferList_[10];
};

4:28: error: assigning to an array from an initializer list

MilGrabBufferList_ = {0};

However, this code compiles without error:

class CFrameProd{
public:
CFrameProd(){}
private:
long MilGrabBufferList_[10]={0};
};

Class member initialisation is used here.

The original error occurred because you cannot assign to an array after its declaration.

(there is always the option of using an initialiser list: CFrameProd(): MilGrabBufferList_{0}{})

How do I enable C++11 in gcc?

H2CO3 is right, you can use a makefile with the CXXFLAGS set with -std=c++11
A makefile is a simple text file with instructions about how to compile your program. Create a new file named Makefile (with a capital M). To automatically compile your code just type the make command in a terminal. You may have to install make.

Here's a simple one :

CXX=clang++
CXXFLAGS=-g -std=c++11 -Wall -pedantic
BIN=prog

SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)

all: $(OBJ)
$(CXX) -o $(BIN) $^

%.o: %.c
$(CXX) $@ -c $<

clean:
rm -f *.o
rm $(BIN)

It assumes that all the .cpp files are in the same directory as the makefile. But you can easily tweak your makefile to support a src, include and build directories.

Edit : I modified the default c++ compiler, my version of g++ isn't up-to-date. With clang++ this makefile works fine.

Compiling C++11 in Visual Studio Code

Go to Settings > User Settings
In here, search for Run Code Configuration:

Under this menu, find:
"code-runner.executorMap"

Edit this Setting by adding it to User Setting as below for C++11 support:

"code-runner.executorMap":{
"cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},

OR

Edit this Setting by adding it to User Setting as below for C++17 support:

"code-runner.executorMap":{
"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},

Hope this helps !

Compiling C++11 code on older versions of gcc

A general solution to writing programs that are portable across dialects of C++, whether that is for different standard versions or for language extensions is to wrap the feature in a macro:

#if __cplusplus >= 201103L
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif

Test::~Test() NOEXCEPT

However, destructors are implicitly noexcept (unless a sub-object has a potentially throwing destructor), so simplest solution in this particular case is to simply remove the noexcept declaration, since that won't change the meaning of the program in C++11.

How to use C++ 20 in g++

I would try updating gcc. C++ 20 was introduced in gcc version 8 which is pretty new.



Related Topics



Leave a reply



Submit