Linux 为什么使用 g++ 而不是 gcc 来编译 *.cc 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6626363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:00:42  来源:igfitidea点击:

why use g++ instead of gcc to compile *.cc files?

linuxgccg++compilation

提问by deepsky

I compiled a library which use the g++ instead gcc. First I thought the source code was written in C++ but I found out later that there was not any C++ code in the *.cc files.

我编译了一个使用 g++ 而不是 gcc 的库。起初我以为源代码是用 C++ 编写的,但后来我发现 *.cc 文件中没有任何 C++ 代码。

To confirm this, I replaced the g++ in the original makefile with gcc. And I still got the correct program.

为了确认这一点,我用 gcc 替换了原始 makefile 中的 g++。我仍然得到了正确的程序。

Anyone can explain this? It was not the first time I met such a situation.

任何人都可以解释这一点?我不是第一次遇到这样的情况。

采纳答案by Matthew Slattery

It depends on what exactly you changed in the makefile. gcc/ g++is really just a front-end driver program which invokes the actual compiler and / or linker based on the options you give it.

这取决于您在 makefile 中究竟更改了什么。 gcc/g++实际上只是一个前端驱动程序,它根据您提供的选项调用实际的编译器和/或链接器。

If you invoke the compiler as gcc:

如果您调用编译器为gcc

  • it will compileas C or C++ based on the file extension (.c, or .cc/ .cpp);
  • it will linkas C, i.e. it will not pull in C++ libraries unless you specifically add additional arguments to do so.
  • 它将根据文件扩展名(, 或/ )编译为 C 或 C++ ;.c.cc.cpp
  • 它将作为 C链接,即它不会拉入 C++ 库,除非您专门添加额外的参数来这样做。

If you invoke the compiler as g++:

如果您调用编译器为g++

  • it will compileas C++ regardless of whether or not the file extension is .cor .cc/ .cpp;
  • it will linkas C++, i.e. automatically pull in the standard C++ libraries.
  • 无论文件扩展名是还是/ ,它都会编译为 C++ ;.c.cc.cpp
  • 它将作为 C++链接,即自动拉入标准 C++ 库。

(see the relevant bit of the GCC documentation).

(请参阅GCC 文档的相关部分)。



Here's a simple program which detects whether or not it has been compiled as C or C++.

这是一个简单的程序,用于检测它是否已编译为 C 或 C++。

(It makes use of the fact that a character constant has the size of an intin C, or a charin C++. sizeof(char)is 1 by definition; sizeof(int)will generally be larger - unless you're using an obscure platform with >= 16-bit bytes, which you're probably not.)

(它利用了这样一个事实,即字符常量int在 C中具有 an 大小,或char在 C++ 中具有 a 大小。 sizeof(char)根据定义为 1;sizeof(int)通常会更大——除非您使用的是 >= 16 位字节的晦涩平台,你可能不是。)

I've called it test.cand copied it as test.ccas well:

我已经调用test.c并复制了它test.cc

$ cat test.c
#include <stdio.h>

int main(void)
{
  printf("I was compiled as %s!\n", sizeof('a') == 1 ? "C++" : "C");
  return 0;
}
$ cp test.c test.cc
$

Compiling and linking test.cwith gcc, and test.ccwith g++, works as expected:

编译和链接test.cgcc,并test.ccg++,按预期工作:

$ gcc -o test test.c
$ ./test
I was compiled as C!
$ g++ -o test test.cc
$ ./test
I was compiled as C++!
$ 

Compiling and linking test.ccwith gccdoesn't work: it compiles the code as C++ because the file ends in .cc, but fails at the link stage:

编译和链接test.ccgcc不工作:它编译代码,C ++,因为文件结束.cc,但在链接阶段失败:

$ gcc -o test test.cc
/tmp/ccyb1he5.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
$ 

which we can prove by separately compiling with gcc, and linking with g++(to pull in the right libraries):

我们可以通过单独编译gcc和链接来证明这一点g++(以引入正确的库):

$ gcc -c test.cc
$ g++ -o test test.o
$ ./test
I was compiled as C++!
$

...gcchas compiled the code as C++ rather than C, because it had a .ccfile extension.

...gcc已将代码编译为 C++ 而不是 C,因为它具有.cc文件扩展名。

Whereas g++does notcompile .cfiles as plain C:

g++没有编译.c文件为纯C:

$ g++ -o test test.c 
$ ./test
I was compiled as C++!
$ 

回答by Dunnie

I don't know why they chose to use g++ instead of gcc, but I believe it shouldn't matter, as any valid C program is also valid C++.

我不知道他们为什么选择使用 g++ 而不是 gcc,但我相信这无关紧要,因为任何有效的 C 程序也是有效的 C++。

回答by spraff

It could be that the .cc code happensto be C, but was intended to be linked into a C++ library. The internals are different.

可能是 .cc 代码恰好是 C,但旨在链接到 C++ 库中。内功不一样。

回答by vines

g++automatically links the C++ runtime library — gccdoesn't. Obvoiusly, when it doesn't matter — then it doesn't matter, but, as already pointed out by spraff, it could be intended for future use.

g++自动链接 C++ 运行时库 —gcc不会。显然,当它无关紧要时——那么它就无关紧要,但是,正如 spraff 已经指出的那样,它可以用于未来的使用。