Compiler Flags

gcc vs g++

The main differences:

  • gcc will compile: .c/.cpp files as C and C++ respectively.

  • g++ will compile: .c/.cpp files but they will all be treated as C++ files.

  • Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).

Compiling

$ clang++-6.0 main.cpp

Searching through clang documentation

clang --help | grep "\-I "

$ clang --help | grep "\-o "
  -o <file>               Write output to <file>

Flags

  • --version

  • -c

  • -std=<standard>

  • -o <file>

  • -D<macroname>=<value>

  • -I<directory>

  • -shared, --shared

--version

-c , --compile

Only run preprocess, compile, and assemble steps no linking generates a target “.o” object file.

-std=<standard>

sets standard library. This must be set if you are to use new features of c++. Use -std=c++2a for c++20

-o <file>

The default name of the output file is a.out but this can be changed

-D<macroname>=<value>

The macro flag

If we have the following file: main.cpp

We can choose which code to run using -D

or this without a space

or running this:

-I<dir>, --include-directory <arg>, --include-directory=<arg>

If we had these 3 files but MyClass.cpp and MyClass.hpp were in a different folder

include/MyClass.hpp

src/MyClass.cpp

src/main.cpp

To compile we would have to do something like this:

This will look for our headers in the include directory. This allows us to organize our files.

A few more important flags

  • -shared, --shared

  • --cuda-path=

g++/gcc specific flag

In order to compile with all warnings enabled and to produce standards-compatible C++ code, I recommend using the flags

more infoarrow-up-right clang more infoarrow-up-right 2nd linkarrow-up-right

Last updated