What's the difference between gcc and g++/gcc-c++?
在我看来,GCC可以同时处理C和C++项目,那么为什么需要G++/GCC-C++?
G++和GCC-C++有什么区别?
如果文件具有适当的扩展,EDCOX1 0将编译C源文件作为C++的C++源文件,但它不会自动在C++库中链接。
EDCOX1(1)将自动包含C++库;默认情况下,它还会用扩展名编译文件,这些文件表示它们是C++源,而不是C。
从http://gcc.gnu.org/onlinedocs/gcc/invoking-g_u 002b.html invoking-g b_u 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.
号
例如,为了编译一个简单的C++程序,它可以写入EDCOX1×2流,我可以使用(MinGW在Windows上):
- g++-o test.exe test.cpp测试.cpp
- gcc-o test.exe test.cpp-lstdc++
但如果我尝试:
- gcc-o test.exe测试.cpp
我在链接时得到未定义的引用。
另一个区别是,以下C程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <stdlib.h> #include <stdio.h> int main() { int* new; int* p = malloc(sizeof(int)); *p = 42; new = p; printf("The answer: %d ", *new); return 0; } |
编译并运行良好,使用:
- gcc-o test.exe测试.c
但在编译时会出现以下错误:
- g++-o test.exe测试.c
错误:
1 2 3 4 5 6 7 | 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 |
号
据我所知,G++使用正确的C++链接器选项,而GCC使用C链接器选项(因此,您可能会获得未定义的引用等)。