What is the difference between g++ and gcc?

后端 未结 10 2165
无人共我
无人共我 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:46

    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.

    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.

    0 讨论(0)
  • 2020-11-22 00:47

    One notable difference is that if you pass a .c file to gcc it will compile as C.

    The default behavior of g++ is to treat .c files as C++ (unless -x c is specified).

    0 讨论(0)
  • 2020-11-22 00:47

    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 :

    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

    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.

    0 讨论(0)
  • 2020-11-22 00:49

    gcc and g++ are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the 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.

    The probably most important difference in their defaults is which libraries they link against automatically.

    According to GCC's online documentation link options and 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).

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

    0 讨论(0)
  • 2020-11-22 00:50

    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.

    0 讨论(0)
提交回复
热议问题