为什么使用'g++'而不是“gcc”;编译 *.cc 文件?
我编译了一个使用 g++
而不是 gcc
的库。一开始我以为源代码是用C++写的,后来发现*.cc文件里并没有任何C++代码。
为了确认这一点,我用 gcc
替换了原始 makefile 中的 g++
。而且我仍然得到了正确的程序。
解释是什么?我不是第一次遇到这样的情况。
I compiled a library which used g++
instead of 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.
To confirm this, I replaced the g++
in the original makefile with gcc
. And I still got the correct program.
What is the explanation? It was not the first time I met such a situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这取决于您在 makefile 中到底更改了什么。
gcc
/g++
实际上只是一个前端驱动程序,它根据您提供的选项调用实际的编译器和/或链接器。如果您以
gcc
形式调用编译器:.c
或.c
)编译为 C 或 C++。 cc/.cpp
);如果您以
g++
方式调用编译器:.c
还是,它都会编译为 C++。 cc
/.cpp
;(参见 GCC 的相关位文档)。
这是一个简单的程序,用于检测它是否已编译为 C 或 C++。
(它利用了字符常量的大小相当于 C 中的
int
或 C++ 中的char
的事实。sizeof(char)
根据定义为 1;sizeof(int)
通常会更大 - 除非您使用的是 >= 16 位字节的模糊平台,但您可能不会。)将其命名为
test.c
并复制它作为test.cc
以及:文件 test.c
复制
编译并链接
test.c
与gcc
,以及test.cc
与g++
一起按预期工作:使用
gcc
编译和链接test.cc
不起作用:它将代码编译为 C++,因为文件以.cc
,但在链接阶段失败:输出:
我们可以通过单独用
gcc
编译,并用g++
链接来证明(以拉取在正确的库中):...
gcc
已将代码编译为 C++ 而不是 C,因为它具有.cc
文件扩展名。而
g++
确实不将.c
文件编译为纯 C: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.If you invoke the compiler as
gcc
:.c
, or.cc
/.cpp
);If you invoke the compiler as
g++
:.c
or.cc
/.cpp
;(see the relevant bit of the GCC documentation).
Here's a simple program which detects whether or not it has been compiled as C or C++.
(It makes use of the fact that a character constant has the size of an
int
in C, or achar
in 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.)I've called it
test.c
and copied it astest.cc
as well:File test.c
Copy
Compiling and linking
test.c
withgcc
, andtest.cc
withg++
, works as expected:Compiling and linking
test.cc
withgcc
doesn't work: it compiles the code as C++ because the file ends in.cc
, but fails at the link stage:Output:
which we can prove by separately compiling with
gcc
, and linking withg++
(to pull in the right libraries):...
gcc
has compiled the code as C++ rather than C, because it had a.cc
file extension.Whereas
g++
does not compile.c
files as plain C:.cc 代码可能是 C,但其目的是链接到 C++ 库中。内部结构不同。
It could be that the .cc code happens to be C, but was intended to be linked into a C++ library. The internals are different.
g++
会自动链接 C++ 运行时库,而gcc
不会。显然,当它不重要时,那就不重要了,但是,正如已经spraff指出,它可能供将来使用。g++
automatically links the C++ runtime library—gcc
doesn't. Obviously, 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++ 而不是 gcc,但我相信这不重要,因为任何有效的 C 程序也是有效的 C++。
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++.