C++ g++ 和 gcc 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/172587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between g++ and gcc?
提问by Brian R. Bondy
What is the difference between g++ and gcc? Which one of them should be used for general c++ development?
g++ 和 gcc 有什么区别?其中哪一个应该用于一般的 C++ 开发?
采纳答案by Brian R. Bondy
gcc
and g++
are compiler-drivers of the GNU Compiler Collection(which was once upon a time just the GNU C Compiler).
gcc
并且g++
是 GNU Compiler Collection(从前只是 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.
尽管它们会cc1
cc1plus
根据文件类型自动确定要调用的后端 ( ...),但除非被 覆盖,否则-x language
它们还是存在一些差异。
The probably most important difference in their defaults is which libraries they link against automatically.
它们的默认值可能最重要的区别是它们自动链接到哪些库。
According to GCC's online documentation link optionsand how g++ is invoked, g++
is 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).
根据 GCC 的在线文档链接选项和如何调用 g++,g++
相当于gcc -xc++ -lstdc++ -shared-libgcc
(第一个是编译器选项,第二个是链接器选项)。这可以通过使用-v
选项运行两者来检查(它显示正在运行的后端工具链命令)。
回答by Martin York
GCC: GNU Compiler Collection
GCC:GNU 编译器集合
- Referrers to all the different languages that are supported by the GNU compiler.
- 引用 GNU 编译器支持的所有不同语言。
gcc: GNU C Compiler
g++: GNU C++ Compiler
gcc:GNU C 编译器
g++:GNU C++ 编译器
The main differences:
主要区别:
- gcc will compile: *.c/*.cpp files as C and C++ respectively.
- g++ will compile: *.c/*.cpp files but they will all be treated as C++ files.
- Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
- gcc compiling C files has fewer predefined macros.
- gcc compiling *.cpp and g++ compiling *.c/*.cpp files has a few extra macros.
- gcc 将编译: *.c/*.cpp 文件分别为 C 和 C++。
- g++ 将编译: *.c/*.cpp 文件,但它们都将被视为 C++ 文件。
- 此外,如果您使用 g++ 链接目标文件,它会自动链接到 std C++ 库中(gcc 不会这样做)。
- gcc 编译 C 文件具有较少的预定义宏。
- gcc 编译 *.cpp 和 g++ 编译 *.c/*.cpp 文件有一些额外的宏。
Extra Macros when compiling *.cpp files:
编译 *.cpp 文件时的额外宏:
#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern
回答by Nils Pipenbrinck
For c++ you should use g++.
对于 c++,你应该使用 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.
它是相同的编译器(例如 GNU 编译器集合)。GCC 或 G++ 只需选择具有不同默认选项的不同前端即可。
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).
简而言之:如果您使用 g++,前端会告诉链接器您可能想要链接 C++ 标准库。gcc 前端不会这样做(如果您传递正确的命令行选项,它也可以与它们链接)。
回答by l --marc l
What is the difference between g++
and gcc
?
g++
和 和有gcc
什么区别?
gcc
has evolved from a single language "GNU C Compiler" to be a multi-language "GNU Compiler Collection". The term "GNU C Compiler" is still used sometimes in the context of C programming.
gcc
已经从单一语言“GNU C Compiler”演变为多语言“GNU Compiler Collection”。术语“GNU C 编译器”有时仍用于 C 编程的上下文中。
The g++
isthe C++ compiler for the GNU Compiler Collection. Like gnat
is the Ada compiler for gcc
. see Using the GNU Compiler Collection (GCC)
该g++
是对GNU编译器的C ++编译器。就像gnat
是艾达编译器gcc
。请参阅使用 GNU 编译器集合 (GCC)
For example, the Ubuntu 16.04 and 18.04 man g++
command returns the GCC(1)
manual page.
例如,Ubuntu 16.04 和 18.04man g++
命令返回GCC(1)
手册页。
The Ubuntu 16.04 and 18.04 man gcc
states that ...
Ubuntu 16.04 和 18.04man gcc
声明......
g++
accepts mostly the same options asgcc
g++
接受大部分相同的选项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.
... 使用
gcc
不添加 C++ 库。g++
是一个调用 GCC 并自动指定针对 C++ 库的链接的程序。除非使用 -x,否则它将 .c、.h 和 .i 文件视为 C++ 源文件而不是 C 源文件。该程序在预编译带有 .h 扩展名的 C 头文件以用于 C++ 编译时也很有用。
Search the gcc
man pages for more details on the option variances between gcc
and g++
.
搜索gcc
手册页以获取有关gcc
和之间选项差异的更多详细信息g++
。
Which one should be used for general c++ development?
一般的c++开发应该使用哪一个?
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.
从技术上讲,gcc
或者g++
可以用于具有适用选项设置的一般 C++ 开发。但是,g++
默认行为自然与 C++ 开发保持一致。
The Ubuntu 18.04 man pageadded the following paragraph:
在Ubuntu的18.04手册页添加了以下段落:
The usual way to run GCC is to run the executable called
gcc
, ormachine-gcc
when cross-compiling, ormachine-gcc-version
to run a specific version of GCC. When you compile C++ programs, you should invoke GCC asg++
instead.
运行 GCC 的常用方法是运行名为 的可执行文件
gcc
,或者machine-gcc
在交叉编译时machine-gcc-version
运行,或者运行特定版本的 GCC。 当你编译 C++ 程序时,你应该调用 GCC 作为g++
替代。
回答by njsf
One notable difference is that if you pass a .c
file to gcc it will compile as C.
一个显着的区别是,如果您将.c
文件传递给 gcc,它将编译为 C。
The default behavior of g++ is to treat .c
files as C++ (unless -x c
is specified).
g++ 的默认行为是将.c
文件视为 C++(除非-x c
指定)。
回答by zaphod
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.
尽管 gcc 和 g++ 命令做的事情非常相似,但 g++ 被设计为你用来编译 C++ 程序的命令;它旨在自动做正确的事情。
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.
在幕后,它们实际上是同一个程序。据我了解,两者都根据文件扩展名决定是将程序编译为 C 还是 C++。两者都能够链接到 C++ 标准库,但默认情况下只有 g++ 这样做。因此,如果您有一个用 C++ 编写的程序,并且碰巧不需要链接标准库,那么 gcc 会做正确的事情;但是,g++ 也是如此。所以真的没有理由不使用 g++ 进行一般的 C++ 开发。
回答by bruziuz
I became interested in the issue and perform some experiments
我对这个问题感兴趣并进行了一些实验
I found that description here, but it is very short.
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
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
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]
But this one still work with no errors
$ gcc -x c++ -std=c++98 test_cpp.c
And this also
$ g++ -std=c++0x test_cpp_11.cpp
我在这里找到了那个描述,但它很短。
然后我尝试在我的 Windows 机器上尝试 gcc.exe 和 g++.exe:
$ 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
我试图编译 c89、c99 和 c++1998 简单的测试文件,它对我来说工作得很好,具有与语言匹配的正确扩展名
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
但是当我尝试以这种方式运行“gnu 编译器集合”工具时:
$ 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]
但是这个仍然没有错误
$ gcc -x c++ -std=c++98 test_cpp.c
而这也
$ g++ -std=c++0x test_cpp_11.cpp
p.s. Test files
ps 测试文件
$ 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:
发现:
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
gcc works fine as metatool for if you have correct extension or set correct -std -x flags. See this
如果查看进程树,那么 gcc 和 g++ 似乎是其他工具的后端,在我的环境中是:cc1plus.exe、cc1.exe、collect2.exe、as.exe、ld.exe
如果您有正确的扩展名或设置正确的 -std -x 标志,gcc 可以作为元工具正常工作。看到这个
回答by Minh-Triet Pham Tran
“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”).
“GCC”是 GNU Compiler Collection 的常用简写术语。这既是编译器最通用的名称,也是在强调编译 C 程序时使用的名称(作为以前代表“GNU C 编译器”的缩写)。
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.
在提到 C++ 编译时,通常将编译器称为“G++”。由于只有一个编译器,所以无论什么语言上下文,称之为“GCC”也是准确的;然而,当重点是编译 C++ 程序时,术语“G++”更有用。
You could read more here.
你可以在这里阅读更多。
回答by magicmax
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 :
我在 linux 系统中测试 gcc 和 g++。通过使用 MAKEFILE,我可以定义“GNU make”使用的编译器。我使用“C plus plus”的所谓“动态内存”定位功能进行了测试:
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
在我的电脑上只有g++可以成功编译,而gcc会报错
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.
所以我自己的结论是gcc不完全支持“C plus plus”。似乎为 C++ 源文件选择 g++ 是一个更好的选择。
回答by Marcus Thornton
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.
gcc 和 g++ 都是 GNU 编译器。它们都编译 c 和 c++。不同之处在于,对于*.c 文件,gcc 将其视为 ac 程序,而 g++ 将其视为 ac ++ 程序。*.cpp 文件被认为是 c ++ 程序。c++是c的超集,语法更严格,要注意后缀。