Performance difference between gcc and g ++ for C program

Let's say I wrote a program in C and compiled it with both gcc and g ++, compiling it will work faster? gcc or g ++? I think compiling g ++ will make it slow, but not sure about that.

Let me clarify my question again because of the confusion about gcc.

Say I'm compiling an ac program like this on the console.

gcc ac g++ ac 

which a.out will work faster?

+7
c gcc g ++
source share
6 answers

http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/G_002b_002b-and-GCC.html

GCC is a compiler compilation. It is mainly used to compile C, C ++, Ada, Java, and many other programming languages. g ++ is part of the gnu compiler (gcc) compilation.
I mean gcc includes g ++. When we use gcc to compile C ++, it uses g ++. The output files will be different because the g ++ compiler uses its own runtime library.

Edit: Good to clarify the situation, because we have a bit of confusion about the name here. GCC is a compilation of GNU compilers. It can compile Ada, C ++, C and a billion and a half other languages. This is the backend for the front end compilers of various languages ​​such as GNAT. Go read the link I made at the top of the page from GCC.GNU.Org.

GCC can also reference the GNU C compiler. This will compile C ++ code if the -lstdC ++ command is given, but will usually suffocate and die because it does not pull out C ++ libraries.

g ++, a GNU C ++ compiler, such as the GNU C compiler, is an interface to the GNU compiler collection. The difference between the C compiler is that it automatically includes these libraries and makes several other small adjustments, because it is assumed that it will be passed C ++ code for compilation.

This is where the confusion arises. Does this clarify the situation a bit?

-one
source share

First: the question (and some other answers) seems to be based on the erroneous premise that C is a strict subset of C ++, which is actually not the case. Compiling C as C ++ is not the same as compiling it as C: it can change the meaning of your program!

C will basically compile as C ++ and will basically produce the same results, but there are some things that are explicitly defined to give different behavior.

Here is a simple example - if this is your ac :

 #include <stdio.h> int main(void) { printf("%d\n", sizeof('x')); return 0; } 

then compiling in the form of C will give one result:

 $ gcc ac $ ./a.out 4 

and compilation, since C ++ will give a different result (unless you use an unusual platform, where int and char are the same size):

 $ g++ ac $ ./a.out 1 

because the C specification defines a character literal of type int , and the C ++ specification defines its char type.

Secondly: gcc and g++ are not "the same compiler." The same end code is used, but the front ends of C and C ++ are different pieces of code ( gcc/c-*.c and gcc/cp/*.c in the gcc source).

Even if you stick to parts of the language that are defined for the same, there is no guarantee that the front of C ++ will parse the code in the same way as the front of C (i.e. the same input to the back end), and, therefore, does not guarantee that the generated code will be identical. Thus, it is possible that in some cases it is possible to generate faster code than another, although I would suggest that you need complex code in order to have at least some chance to find the difference, since most of the optimization and code generation occurs in general end of compiler; and the difference can be a round anyway.

+24
source share

I think that both of them will produce the same machine code and therefore the same speed on your computer.

If you want to know, you can compile the assembly for both and compare these two, but I am sure that they create the same assembly and therefore the same machine code.

+2
source share

Profile it and try it. I am sure that this will depend on the actual code, even if it would require a potentially real strange case to get any other bytecode. Although, if you don't have extern C {} around your C code, and also works fine in C, I'm not sure how β€œcompiling it as if it was C ++” could provide any speed, if only specific compiler optimization in g ++ will just turn out to be a little better for your specific situation ...

+2
source share

The generated machine code must be identical. The version of g.g ++ g ++ will probably be bundled with several additional support libraries. This will cause a.out startup time to be slower with a few system calls.

However, in reality there is no practical difference. The Linux linker will not become noticeably slower until you reach 20-40 linked libraries and thousands of characters for resolution.

+2
source share

gcc and g ++ executables are just interfaces, they are not real compilers. They both run the actual C or C ++ compilers (and ld, ar, regardless of what is needed to get your output) based on file extensions. This way you get the same result. g ++ is commonly used for C ++ as it is linked to the standard C ++ library (iostreams, etc.).

If you want to compile C code as C ++, change the file extension or do something like this:

 gcc test.c -otest -x c ++
+2
source share

All Articles