G++有什么区别和GCC?

发布于 2025-02-05 17:47:25 字数 38 浏览 2 评论 0 原文

G ++和GCC有什么区别?其中哪一个应用于一般C ++开发?

What is the difference between g++ and gcc? Which one of them should be used for general c++ development?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

╭ゆ眷念 2025-02-12 17:47:26

GCC g ++ 是GNU编译器 Collection 的编译器驱动程序(曾经是GNU c编译器)。

即使他们自动确定哪些后端( cc1 cc1plus ...)以根据文件类型来调用,除非用> > x语言 ,他们有一些差异。

他们默认值最重要的区别是它们会自动链接到哪些库。

根据GCC的在线文档链接选项如何调用g ++ g ++ 等效于 GCC -XC ++ -LSTDC ++ -SHARED -LIBGCC (第一个是编译器选项,第二个两个是链接器选项)。可以通过使用 -V 选项运行(它显示正在运行的后端工具链命令)来检查这。

默认情况下(和 GCC 不同), g ++还添加了链接选项 -lm - 以 libm 链接,其中包含 Math.h的实现

gcc and g++ are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler).

Even though they automatically determine which backends (cc1 cc1plus ...) to call depending on the file-type, unless overridden with -x language, they have some differences.

The probably most important difference in their defaults is which libraries they link against automatically.

According to GCC's online documentation link options and how g++ is invoked, g++ is roughly equivalent to gcc -xc++ -lstdc++ -shared-libgcc (the 1st is a compiler option, the 2nd two are linker options). This can be checked by running both with the -v option (it displays the backend toolchain commands being run).

By default (and unlike gcc), g++ also adds linker option -lm -- to link against libm which contains implementations for math.h.

半仙 2025-02-12 17:47:26

GCC:GNU编译器集合

  • 转介商对GNU编译器支持的所有不同语言。

GCC :GNU C       编译器
G ++ :GNU C ++编译器

主要区别:

  1. GCC 将编译:*。c \*。cpp cpp 分别为c和c ++ 。
  2. g ++ 将编译:*。c \*。cpp 文件,但它们都将被视为C ++文件。
  3. 另外,如果您使用 g ++ 链接它自动在STD C ++库中链接的对象文件( GCC 不执行此操作)。
  4. GCC 编译C文件的预定义宏更少。
  5. GCC 编译*。cpp g ++ 编译*。c \*。cpp 文件具有一些额外的宏。

编译*。cpp 文件时的额外宏:

#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern

GCC: GNU Compiler Collection

  • Referrers to all the different languages that are supported by the GNU compiler.

gcc: GNU C      Compiler
g++: GNU C++ Compiler

The main differences:

  1. gcc will compile: *.c\*.cpp files as C and C++ respectively.
  2. g++ will compile: *.c\*.cpp files but they will all be treated as C++ files.
  3. Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
  4. gcc compiling C files has fewer predefined macros.
  5. gcc compiling *.cpp and g++ compiling *.c\*.cpp files has a few extra macros.

Extra Macros when compiling *.cpp files:

#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern
野侃 2025-02-12 17:47:26

对于C ++,您应该使用G ++。

它是相同的编译器(例如GNU编译器集合)。 GCC或G ++只需选择具有不同默认选项的不同前端。

简而言之:如果您使用G ++,前端将告诉链接器您可能需要与C ++标准库链接。 GCC前端不会这样做(如果您通过正确的命令行选项,它也可以与它们链接)。

For c++ you should use g++.

It's the same compiler (e.g. the GNU compiler collection). GCC or G++ just choose a different front-end with different default options.

In a nutshell: if you use g++ the frontend will tell the linker that you may want to link with the C++ standard libraries. The gcc frontend won't do that (also it could link with them if you pass the right command line options).

忘年祭陌 2025-02-12 17:47:26

之间有什么区别

g ++ gcc gcc 成为多语言“ GNU编译器集合”。术语 GCC 有时仍会在C编程的上下文中引用“ GNU C编译器”。

man gcc

# GCC(1)                     GNU
# 
# NAME
#        gcc - GNU project C and C++ compiler

但是, g ++ GNU编译器集合的C ++编译器前端。像 gnat gcc 的ADA编译器前端。 请参阅使用GNU Compiler Collection(GCC) /a>

例如,ubuntu 16.04和18.04 man g ++ 命令返回 gcc(1)手动页面。

Ubuntu 16.04和18.04 MAN GCC 指出...

g ++ 主要接受与 gcc

相同的选项

g ++ 主要接受与 gcc 和默认值

...使用 GCC 不添加C ++库。 G ++ 是一个程序
该调用GCC并自动指定针对C ++的链接
图书馆。它将.c,.h和.i将其视为C ++源文件而不是
C源文件除非使用-x。当此程序也很有用
将C标头文件进行预编译,并使用.H扩展名用于C ++
汇编。

搜索 GCC MAN页面以获取有关 GCC g ++ 之间选项差异的更多详细信息。

从技术上讲,应该将哪个用于一般C ++开发?

gcc g ++ 可以用于带有适用选项设置的常规C ++开发。但是, g ++ 默认行为自然与C ++开发一致。

ubuntu 20.04 ubuntu 24.04 继续有以下段落:

运行GCC的常用方法是运行可执行的可执行文件,称为 gcc Machine-GCC 在交叉编译时或 Machine> Machine-gcc-version < /代码>运行GCC的特定版本。 编译C ++程序时,应将GCC称为 g ++ 而不是。



。 > g ++ 只需链接到 gcc 因此, g ++ 调用可能会在每个脚链的基础上有所不同。

ls -l /Applications/Xcode.app/Contents/Developer/usr/bin
# …
# lrwxr-xr-x  1 root  wheel         3 Apr 27  2021 g++ -> gcc
# -rwxr-xr-x  1 root  wheel    167120 Nov 23 20:51 gcc

What is the difference between g++ and gcc?

gcc has evolved from a single language "GNU C Compiler" to be a multi-language "GNU Compiler Collection". The term gcc may still sometimes refer to the "GNU C Compiler" in the context of C programming.

man gcc

# GCC(1)                     GNU
# 
# NAME
#        gcc - GNU project C and C++ compiler

However, g++ is the C++ compiler frontend for the GNU Compiler Collection. Like gnat is the Ada compiler frontend for gcc. see Using the GNU Compiler Collection (GCC)

For example, the Ubuntu 16.04 and 18.04 man g++ command returns the GCC(1) manual page.

The Ubuntu 16.04 and 18.04 man gcc states that ...

g++ accepts mostly the same options as gcc

and that the default ...

... use of gcc does not add the C++ library. g++ is a program
that calls GCC and automatically specifies linking against the C++
library. It treats .c, .h and .i files as C++ source files instead of
C source files unless -x is used. This program is also useful when
precompiling a C header file with a .h extension for use in C++
compilations.

Search the gcc man pages for more details on the option variances between gcc and g++.

Which one should be used for general c++ development?

Technically, either gcc or g++ can be used for general C++ development with applicable option settings. However, the g++ default behavior is naturally aligned to a C++ development.

The Ubuntu 18.04 'gcc' man page added, and Ubuntu 20.04Ubuntu 24.04 continue to have, the following paragraph:

The usual way to run GCC is to run the executable called gcc, or machine-gcc when cross-compiling, or machine-gcc-version to run a specific version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead.


Side Note: In the case of the Xcode.app toolchain, g++ simply links to gcc. Thus, g++ invocations may vary on a per-toolchain basis.

ls -l /Applications/Xcode.app/Contents/Developer/usr/bin
# …
# lrwxr-xr-x  1 root  wheel         3 Apr 27  2021 g++ -> gcc
# -rwxr-xr-x  1 root  wheel    167120 Nov 23 20:51 gcc
冰葑 2025-02-12 17:47:26

一个值得注意的区别是,如果您将 .c 文件传递给GCC,则将编译为C。G

++的默认行为是将 .c .c 文件视为C ++(除非<<<<<指定代码> -x c )。

One notable difference is that if you pass a .c file to gcc it will compile as C.

The default behavior of g++ is to treat .c files as C++ (unless -x c is specified).

失退 2025-02-12 17:47:26

尽管GCC和G ++命令做非常相似的事情,但G ++设计为您调用以编译C ++程序的命令。它旨在自动做正确的事情。

在幕后,它们确实是相同的程序。据我了解,两者都根据文件扩展决定是否将程序编译为C还是C ++。两者都可以链接到C ++标准库,但默认情况下只有G ++才能进行。因此,如果您有一个在C ++中编写的程序,而该程序恰好需要与标准库链接,那么GCC将碰巧做正确的事情。但是,G ++也会。因此,确实没有理由不将G ++用于一般C ++开发。

Although the gcc and g++ commands do very similar things, g++ is designed to be the command you'd invoke to compile a C++ program; it's intended to automatically do the right thing.

Behind the scenes, they're really the same program. As I understand, both decide whether to compile a program as C or as C++ based on the filename extension. Both are capable of linking against the C++ standard library, but only g++ does this by default. So if you have a program written in C++ that doesn't happen to need to link against the standard library, gcc will happen to do the right thing; but then, so would g++. So there's really no reason not to use g++ for general C++ development.

谜泪 2025-02-12 17:47:26

我对这个问题产生了兴趣,并执行了一些实验

  1. 我发现描述在这里,但是很短。

  2. 然后,我尝试在Windows机器上尝试gcc.exe和g ++。exe:

      $ g ++  -  version |头-N1 
    G ++。EXE(GCC-4.6.3用补丁发布[build 20121012 by perlmingw.sf.net])4.6.3
    
    $ gcc- version |头-N1
    gcc.exe(gcc-4.6.3用补丁发布[build 20121012 by perlmingw.sf.net])4.6.3
     
  3. 我尝试编译C89,C99和C ++ 1998 Simple Test Files,并且非常适合我的语言匹配的正确扩展名

      gcc -std = c99 test_c99.c
    GCC -STD = C89 test_c89.c 
    G ++ -STD = C ++ 98 test_cpp.cpp
    GCC -STD = C ++ 98 test_cpp.cpp
     
  4. 的正确扩展名,但是当我尝试运行“ GNU编译器集合”时,以这种方式的工具:

      $ GCC -STD = C ++ 98 TEST_CPP.C
    cc1.exe:警告:命令行选项'-std = C ++ 98'有效于C ++/OBJC ++,但对于C [默认启用]
     

  5. ,但仍然没有错误

      $ gcc -x c ++ -std = C ++ 98 test_cpp.c
     

  6. ,这也是

      $ g ++ -std = C ++ 0x test_cpp_11.cpp 
     

P.S.测试文件

$ cat test_c89.c test_c99.c test_cpp.cpp

// C89 compatible file
int main()
{
    int x[] = {0, 2};
    return sizeof(x);
}

// C99 compatible file
int main()
{
    int x[] = {[1]=2};
    return sizeof(x);
}

// C++1998,2003 compatible file
class X{};
int main()
{
    X x;
    return sizeof(x);
}

// C++11
#include <vector>
enum class Color : int{red,green,blue}; // scoped enum
int main()
{
    std::vector<int> a {1,2,3}; // bracket initialization
    return 0;
}

调查结果:

  1. 如果查看过程树,则GCC和G ++似乎是后端到其他工具,在我的环境中是: cc1plus.exe,cc1.exe, collect2.exe,as.exe,ld.exe

  2. gcc gcc可以作为metatoot效果,如果您有正确的扩展名或正确设置
    -STD -X标志。请参阅 this

I became interested in the issue and perform some experiments

  1. I found that description here, but it is very short.

  2. Then I tried to experiment with gcc.exe and g++.exe on my windows machine:

    $ g++ --version | head -n1 
    g++.exe (gcc-4.6.3 release with patches [build 20121012 by perlmingw.sf.net]) 4.6.3
    
    $ gcc --version | head -n1
    gcc.exe (gcc-4.6.3 release with patches [build 20121012 by perlmingw.sf.net]) 4.6.3
    
  3. I tried to compile c89, c99, and c++1998 simple test files and It's work well for me with correct extensions matching for language

    gcc -std=c99 test_c99.c
    gcc -std=c89 test_c89.c 
    g++ -std=c++98 test_cpp.cpp
    gcc -std=c++98 test_cpp.cpp
    
  4. But when I try to run "gnu compiler collection" tool in that fashion:

    $ gcc -std=c++98 test_cpp.c
    cc1.exe: warning: command line option '-std=c++98' is valid for C++/ObjC++ but not for C [enabled by default]
    
  5. But this one still work with no errors

    $ gcc -x c++ -std=c++98 test_cpp.c
    
  6. And this also

    $ g++ -std=c++0x test_cpp_11.cpp 
    

p.s. Test files

$ cat test_c89.c test_c99.c test_cpp.cpp

// C89 compatible file
int main()
{
    int x[] = {0, 2};
    return sizeof(x);
}

// C99 compatible file
int main()
{
    int x[] = {[1]=2};
    return sizeof(x);
}

// C++1998,2003 compatible file
class X{};
int main()
{
    X x;
    return sizeof(x);
}

// C++11
#include <vector>
enum class Color : int{red,green,blue}; // scoped enum
int main()
{
    std::vector<int> a {1,2,3}; // bracket initialization
    return 0;
}

Findings:

  1. If look at process tree then it seems that gcc, and g++ is backend to other tools, which in my environment are: cc1plus.exe, cc1.exe, collect2.exe, as.exe, ld.exe

  2. gcc works fine as metatool for if you have correct extension or set correct
    -std -x flags. See this

究竟谁懂我的在乎 2025-02-12 17:47:26

来自使用GNU Compiler Collection(GCC)

“ GCC”是GNU编译器集合的常见速记术语。这
既是编译器最通用的名称,也是使用的名称
重点是编译C程序(作为缩写以前
代表“ GNU C编译器”)。

参考C ++编译时,通常打电话给编译器
“ G ++”。由于只有一个编译器,因此可以准确致电
无论语言上下文如何,它都会“ GCC”;但是,“ g ++”一词
当重点是编译C ++程序时,更有用。

您可以阅读更多在这里

From Using the GNU Compiler Collection (GCC):

“GCC” is a common shorthand term for the GNU Compiler Collection. This
is both the most general name for the compiler, and the name used when
the emphasis is on compiling C programs (as the abbreviation formerly
stood for “GNU C Compiler”).

When referring to C++ compilation, it is usual to call the compiler
“G++”. Since there is only one compiler, it is also accurate to call
it “GCC” no matter what the language context; however, the term “G++”
is more useful when the emphasis is on compiling C++ programs.

You could read more here.

我还不会笑 2025-02-12 17:47:26

GCC和G ++都是GNU编译器。它们都编译C和C ++。 *.c文件GCC将其视为AC程序,而G ++将其视为AC ++程序。 *.CPP文件被认为是C ++程序。 C ++是一组C ++,语法更严格,因此请注意后缀。

gcc and g ++ are both GNU compiler. They both compile c and c++. The difference is for *.c files gcc treats it as a c program, and g++ sees it as a c ++ program. *.cpp files are considered to be c ++ programs. c++ is a super set of c and the syntax is more strict, so be careful about the suffix.

血之狂魔 2025-02-12 17:47:26

我正在Linux系统中测试GCC和G ++。通过使用MakeFile,我可以定义“ GNU Make”使用的纯正者。我用所谓的“动态内存”定位“ C Plus”的功能进行了测试:

int main(){

int * myptr = new int;
* myptr = 1;
printf("myptr[0] is %i\n",*myptr);
return 0;
}

只有G ++可以在我的计算机上成功编译,而GCC会报告错误

undefined reference to `operator new(unsigned long)'

,因此我自己的结论是GCC不完全支持“ C Plus”。 似乎为C ++源文件选择G ++是一个更好的选择。

I was testing gcc and g++ in a linux system. By using MAKEFILE, I can define the compliler used by "GNU make". I tested with the so called "dynamic memory" locating feature of "C plus plus" by :

int main(){

int * myptr = new int;
* myptr = 1;
printf("myptr[0] is %i\n",*myptr);
return 0;
}

Only g++ can successfully compile on my computer while gcc will report error

undefined reference to `operator new(unsigned long)'

So my own conclusion is gcc does not fully support "C plus plus". It seems that choosing g++ for C++ source files is a better option.

九命猫 2025-02-12 17:47:26

G ++和GCC都是GNU编译器Collection(GCC)提供的编译器,主要区别在于它们主要设计用于编译的语言。

GCC:

GCC是C的GNU编译器。它主要用于编译C程序。
当您使用GCC编译源文件时,它假设代码是用C ++编写的

G ++是C ++的GNU编译器。它是专门为编译C ++程序而设计的。
当您使用G ++编译源文件时,它将代码视为C ++代码,从而启用C ++功能和标准。

g++ and gcc are both compilers provided by the GNU Compiler Collection (GCC), and the main difference lies in the languages they are primarily designed to compile.

gcc:

gcc is the GNU Compiler for C. It is primarily used for compiling C programs.
When you use gcc to compile a source file, it assumes that the code is written in C.

g++:

g++ is the GNU Compiler for C++. It is specifically designed for compiling C++ programs.
When you use g++ to compile a source file, it treats the code as C++ code, enabling C++ features and standards.

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