gcc和g ++ / gcc-c ++有什么区别? [英] What's the difference between gcc and g++/gcc-c++?
问题描述
在我看来,gcc可以处理c和c ++项目,为什么需要g ++ / gcc-c ++?
g ++和gcc之间有什么区别-c ++?
g ++
会自动包含C ++库;默认情况下,它也会编译带有扩展名的文件,这些扩展名表明它们是C ++的C ++,而不是C。
来自 http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b :
$ b
C ++源文件通常使用后缀之一 .C
,.cc
,.cpp
,.CPP
,.c ++
,.cp
或.cxx
; C ++头文件通常使用.hh
,.hpp
,.H
或(对于共享模板代码).tcc
;并且预处理的C ++文件使用后缀.ii
。 GCC可以识别具有这些名称的文件,并将它们编译为C ++程序,即使您以编译C程序(通常使用名称gcc)的方式调用编译器。然而,
然而, ,使用gcc不会添加C ++库。 g ++是一个调用GCC并将
.c
,.h
和.i
文件作为C ++源文件而不是C源文件,除非使用-x,并自动指定与C ++库的链接。当使用C ++编译预编译C头文件时,该程序也很有用。
例如,要编译一个写入<$ c $ c> std :: cout 流的简单C ++程序,我可以使用(Windows上的MinGW):
- g ++ -o test.exe test.cpp
- gcc -o test.exe测试。 cpp -lstdc ++
但是,如果我尝试:
我在链接时得到未定义的引用。 b
另一个区别是,下面的C程序:
#include ;
#include< stdio.h>
int main()
{
int * new;
int * p = malloc(sizeof(int));
* p = 42;
new = p;
printf(答案:%d \\\
,* new);
返回0;
}
编译并运行正常:
- gcc -o test.exe test.c
- g ++ -o test.exe test.c
但编译时会出现一些错误使用:
错误:
test.c:函数'int main()':
test .c:6:10:error:expected'unqualified-id before'new'
test.c:6:10:error:expected'initializer before'new'
test.c:7:32:error :无效从'void *'转换为'int *'
test.c:10:9:错误:'='令牌之前的预期类型说明符
test.c:10:11:错误:作为赋值左操作数所需的左值
test.c:12:36:错误:''''令牌之前的预期类型说明符
It seems to me that gcc can deal with both c and c++ projects,so why is g++/gcc-c++ needed?
What's the difference between g++ and gcc-c++?
gcc
will compile C source files as C and C++ source files as C++ if the file has an appropriate extension; however it will not link in the C++ library automatically.
g++
will automatically include the C++ library; by default it will also compile files with extensions that indicate they are C source as C++, instead of as C.
From http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b:
C++ source files conventionally use one of the suffixes
.C
,.cc
,.cpp
,.CPP
,.c++
,.cp
, or.cxx
; C++ header files often use.hh
,.hpp
,.H
, or (for shared template code).tcc
; and preprocessed C++ files use the suffix.ii
. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and treats
.c
,.h
and.i
files as C++ source files instead of C source files unless -x is used, and automatically specifies linking against the C++ library. This program is also useful when precompiling a C header file with a.h
extension for use in C++ compilations.
For example, to compile a simple C++ program that writes to the std::cout
stream, I can use either (MinGW on Windows):
- g++ -o test.exe test.cpp
- gcc -o test.exe test.cpp -lstdc++
But if I try:
- gcc -o test.exe test.cpp
I get undefined references at link time.
And for the other difference, the following C program:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int* new;
int* p = malloc(sizeof(int));
*p = 42;
new = p;
printf("The answer: %d\n", *new);
return 0;
}
compiles and runs fine using:
- gcc -o test.exe test.c
But gives several errors when compiled using:
- g++ -o test.exe test.c
Errors:
test.c: In function 'int main()':
test.c:6:10: error: expected unqualified-id before 'new'
test.c:6:10: error: expected initializer before 'new'
test.c:7:32: error: invalid conversion from 'void*' to 'int*'
test.c:10:9: error: expected type-specifier before '=' token
test.c:10:11: error: lvalue required as left operand of assignment
test.c:12:36: error: expected type-specifier before ')' token
这篇关于gcc和g ++ / gcc-c ++有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!