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

编程入门 行业动态 更新时间:2024-10-27 20:27:40
本文介绍了为什么使用g ++而不是gcc来编译* .cc文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编译了一个使用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 编译和链接 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文件?

本文发布于:2023-10-24 06:00:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   文件   gcc   cc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!