gcc和g ++ / gcc有什么区别

在我看来,gcc可以处理c和c ++项目,为什么需要g ++ / gcc-c ++?

g ++和gcc-c ++有什么区别?


如果文件具有适当的扩展名, gcc会将C源文件作为C和C ++源文件编译为C ++; 但它不会自动链接到C ++库中。

g++将自动包含C ++库; 默认情况下它也会编译带有扩展名的文件,这些扩展名表明它们是C ++的C ++,而不是C。

从http://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html#Invoking-G_002b_002b:

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 ++库的链接。 当预编译带有.h扩展名的C头文件以用于C ++编译时,该程序也很有用。

例如,要编译一个写入std::cout流的简单C ++程序,我可以使用(Windows上的MinGW):

  • g ++ -o test.exe test.cpp
  • gcc -o test.exe test.cpp -lstdc ++
  • 但是,如果我尝试:

  • gcc -o test.exe test.cpp
  • 链接时我收到未定义的参考。

    对于另一个区别,下面的C程序:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main() 
    {
        int* new;
        int* p = malloc(sizeof(int));
    
        *p = 42;
        new = p;
    
        printf("The answer: %dn", *new);
    
        return 0;
    }
    

    编译和运行正常使用:

  • gcc -o test.exe test.c
  • 但是在编译时会出现几个错误:

  • g ++ -o test.exe test.c
  • 错误:

    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链接器选项(所以你可能会得到未定义的引用等)。

    链接地址: http://www.djcxy.com/p/15039.html

    上一篇: What's the difference between gcc and g++/gcc

    下一篇: Difference between CC, gcc and g++?