Is -std=c++2a necessary at link stage as well?

Viewed 1564

If I compile in two stages, using a particular language standard:

g++ -std=c++2a -c file1.cpp                   #compile source files 
g++ -std=c++2a -c file2.cpp
g++ -std=c++2a file1.o file2.o -o program     #link 'em

...can I leave the -std=c++2a out of the link command, or is it sometimes needed?

Version is gcc 10.

1 Answers

I guess you are compiling on Linux with a recent GCC. Be sure to read more about C++ and about your particular compiler (i.e. GCC 9 is not the same as GCC 10). Check with g++ --version what it is.

In practice you want to compile with warnings and debug information (in DWARF for GDB inside your ELF object files and executables), so use

g++ -std=c++2a -Wall -Wextra -g -c file1.cpp

and likewise for file2.cpp

Later (once your program is correct enough, e.g. has few bugs) you could want to ask the compiler to optimize it. So you could use

 g++ -std=c++2a -Wall -Wextra -O3 -g -c file1.cpp

Practically speaking, you'll configure your build automation tool (e.g. GNU make or ninja) to run your compilation commands.

In rare cases, you could want to use link time optimizations. Then you need to both compile and link with g++ -std=c++2a -Wall -Wextra -O3 -g -flto and perhaps other options.

Be aware that link time optimization could almost double your build time.

You could also be interested by static analysis options of GCC 10 (or even by writing your own static analysis using GCC plugins).

Related