What is the difference between g++ and gcc?

后端 未结 10 2169
无人共我
无人共我 2020-11-22 00:34

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

10条回答
  •  难免孤独
    2020-11-22 00:50

    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 
    enum class Color : int{red,green,blue}; // scoped enum
    int main()
    {
        std::vector 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

提交回复
热议问题