为什么使用g ++而不是gcc来编译* .cc文件? [英] why use g++ instead of gcc to compile *.cc files?
问题描述
我编译了一个使用g ++而不是gcc的库。首先我认为源代码是用C ++编写的,但我后来发现,在* .cc文件中没有任何C ++代码。
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.
为了确认这一点,我用gcc替换了原来makefile中的g ++。我还是得到了正确的程序。
To confirm this, I replaced the g++ in the original makefile with gcc. And I still got the correct program.
任何人都可以解释这一点?这不是我第一次遇到这种情况。
Anyone can explain this? It was not the first time I met such a situation.
推荐答案
这取决于你在makefile中改变了什么。 gcc
/ g ++
实际上只是一个前端驱动程序,它调用实际的编译器和/
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.
如果您调用编译器 gcc
:
- 它会根据文件扩展名(
.c
)将编译为C或C ++.cc
/.cpp
); - 作为C,即它不会拉入C ++库,除非你专门添加额外的参数这样做。
- it will compile as C or C++ based on the file extension (
.c
, or.cc
/.cpp
); - it will link as C, i.e. it will not pull in C++ libraries unless you specifically add additional arguments to do so.
调用编译器 g ++
:
- 无论文件扩展名是
.c
还是.cc
/.cpp
; - 会将链接为C ++,即自动拉入标准C ++库。
- it will compile as C++ regardless of whether or not the file extension is
.c
or.cc
/.cpp
; - it will link as C++, i.e. automatically pull in the standard C++ libraries.
(请参阅 GCC文档的相关位)。
这是一个简单的程序,或者不是它已经编译为C或C ++。
Here's a simple program which detects whether or not it has been compiled as C or C++.
(它使用一个字符常量的大小为 int
或C ++中的 char
。 sizeof(char)
根据定义为1; sizeof(int)
通常会更大 - 除非你使用一个含有> = 16位字节的模糊平台,你可能不是这样。)
(It makes use of the fact that a character constant has the size of an int
in C, or a char
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.)
我调用它 test.c
并将其复制为 test.cc
以及:
I've called it test.c
and copied it as test.cc
as well:
$ 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
$
使用 gcc
和 test.cc $ c>编译和链接
test.c
$ c>与 g ++
可按预期工作:
Compiling and linking test.c
with gcc
, and test.cc
with g++
, works as expected:
$ gcc -o test test.c
$ ./test
I was compiled as C!
$ g++ -o test test.cc
$ ./test
I was compiled as C++!
$
编译和链接 test.cc
与 gcc
不工作:它编译代码为C ++,因为文件以 .cc
但在链接阶段失败:
Compiling and linking test.cc
with gcc
doesn't work: it compiles the code as C++ because the file ends in .cc
, but fails at the link stage:
$ gcc -o test test.cc
/tmp/ccyb1he5.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
$
我们可以通过单独编译 gcc
并链接到 g ++
拉入正确的库):
which we can prove by separately compiling with gcc
, and linking with g++
(to pull in the right libraries):
$ gcc -c test.cc
$ g++ -o test test.o
$ ./test
I was compiled as C++!
$
... gcc
将代码编译为C ++而不是C,因为它有一个 .cc
文件扩展名。
...gcc
has compiled the code as C++ rather than C, because it had a .cc
file extension.
code> g ++ 不会将 .c
编译为纯C:
Whereas g++
does not compile .c
files as plain C:
$ g++ -o test test.c
$ ./test
I was compiled as C++!
$
这篇关于为什么使用g ++而不是gcc来编译* .cc文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!