Gcc: Difference Between -O3 and -Os

What is the difference between g++ and gcc?

gcc and g++ are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler).

Even though they automatically determine which backends (cc1 cc1plus ...) to call depending on the file-type, unless overridden with -x language, they have some differences.

The probably most important difference in their defaults is which libraries they link against automatically.

According to GCC's online documentation link options and how g++ is invoked, g++ is equivalent to gcc -xc++ -lstdc++ -shared-libgcc (the 1st is a compiler option, the 2nd two are linker options). This can be checked by running both with the -v option (it displays the backend toolchain commands being run).

Difference between dynamic arrays (GCC) and pointers

  1. foo() allocates the memory on the stack, bar() allocates it on the heap. This has two effects: Lifetime (stack memory is automatically reclaimed at function exit), and max array size (stack space is rather limited to a range of a few MB at most, heap space is only limited by available RAM).

  2. foo() is valid C99, but not C++ of any standard. C++ simply never embraced VLAs. This is the core point where you need to realize that C and C++ are two quite distinct languages. There are valid C programs that are not C++ (like foo()), and there are valid C++ programs that are not C (like bar()). C++ is not the strict superset anymore as which it started.

Nevertheless, compilers may choose to implement a superset of the language as an extension. g++ does so, but if you compile with strict C++ standard compliance (g++ -std=c++14 -pedantic -Werror), even g++ throws an error on foo().

What are the differences between compiling assembly using gcc and as

gcc is just a front-end that runs as (and ld unless you use -c to stop at object files without linking). Use gcc -v to see what it runs and what command line options it passes.

If you want to link with libraries, generally use gcc. It knows the right command-line options to pass to ld to set library paths, and which order to put things in on the ld command line.

You might find gcc -nostdlib or -nostartfiles useful, e.g. if you want to write your own _start but still link libraries. Also -no-pie and/or -static depending on how you want to link.


If you're curious to learn more about the toolchain and linking, then sure play around with ld options and see what breaks when you change the options. And/or use readelf -a to examine the resulting executable.

Difference between cpp and gcc -E

The difference between the two is that gcc -E will eliminate -traditional-cpp. If you include the option then you should receive the same result as cpp.

↳ https://gcc.gnu.org/onlinedocs/cpp/Traditional-Mode.html

Difference between clang and gcc

Yes. And no.

This is like asking whether an Audi car has an advantage over a Mercedes car. Like them, the two compilers are two different projects aiming to do the same thing. In some cases, gcc will emit better code, in others it will be clang.

When you need to know, you have to compile your code with both and then measure it.

There is an argument here and somewhat less related here.



Related Topics



Leave a reply



Submit