为什么使用'g++'而不是“gcc”;编译 *.cc 文件?

发布于 2024-11-18 21:30:05 字数 209 浏览 3 评论 0原文

我编译了一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇女薄笑 2024-11-25 21:30:05

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

如果您以 gcc 形式调用编译器:

  • 它将根据文件扩展名(.c.c编译为 C 或 C++。 cc/.cpp);
  • 它将链接为C,即它不会引入C++库,除非您专门添加额外的参数来这样做。

如果您以 g++ 方式调用编译器:

  • 无论文件扩展名是 .c 还是 ,它都会编译为 C++。 cc / .cpp;
  • 它将链接为C++,即自动引入标准C++库。

(参见 GCC 的相关位文档)。


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

(它利用了字符常量的大小相当于 C 中的 int 或 C++ 中的 char 的事实。sizeof(char) 根据定义为 1;sizeof(int) 通常会更大 - 除非您使用的是 >= 16 位字节的模糊平台,但您可能不会。

)将其命名为 test.c 并复制它作为 test.cc 以及:

文件 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

编译并链接 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++!

使用 gcc 编译和链接 test.cc 不起作用:它将代码编译为 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

我们可以通过单独用 gcc 编译,并用 g++ 链接来证明(以拉取在正确的库中):

$ gcc -c test.cc

$ g++ -o test test.o

$ ./test
I was compiled as C++!

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

g++ 确实.c 文件编译为纯 C:

$ g++ -o test test.c

$ ./test
I was compiled as 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:

  • 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.

If you invoke the compiler as g++:

  • 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.

(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 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.)

I've called it test.c and copied it as test.cc as well:

File test.c

#include <stdio.h>

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

Copy

cp test.c test.cc

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++!

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

Output:

/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 -c test.cc

$ g++ -o test test.o

$ ./test
I was compiled as C++!

...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:

$ g++ -o test test.c

$ ./test
I was compiled as C++!
一抹苦笑 2024-11-25 21:30:05

.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.

给不了的爱 2024-11-25 21:30:05

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.

把时间冻结 2024-11-25 21:30:05

我不知道他们为什么选择使用 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++.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文