skip to Main Content

While learning C++, I tried to compile a HelloWorld program using the ‘gcc’ command’ and found that I needed to add the ‘-lstdc++’ option for it to compile successfully:

gcc HelloWorld.cpp -lstdc++

However, I idly tried to use ‘c++’ as a command to compile a file, and much to my surprise, it worked without me needing to use the -lstdc++ option, and it produced an output executable file that ran just as well as the one produced by the ‘gcc’ command with the ‘-lstdc++’ option:

c++ HelloWorld.cpp

Does anyone know if there are any hidden differences in output between the two commands, and if the ‘c++’ command may be safely used in place of the ‘gcc’ command? I have searched a dozen or so websites, and not a single one of them had any documentation or samples for code featuring ‘c++’ used as a command to compile a C++ executable file in the OS that I’m running (Linux Ubuntu 20.04).

2

Answers


  1. c++ is a soft link of g++.

    Then you can find the difference at this question:What is the difference between g++ and gcc?

    Login or Signup to reply.
  2. Both commands are a part of the GNU Compiler Collection (GCC), among others

    • c++ defaults to assume C++ code as an input source and should be the preferred way to compile C++ code
    • gcc has traditionally been assumed to be a C compiler by default

    Either one can compile C/C++ with appropriate options as you have found out or a mix of the sources. -lstdc++ option exists for C, so when you have parts of the code written in C and parts in C++ to compile with gcc.

    It is certainly safe to use c++ for C++ projects. A man page (man c++) is always a helpful resource to consult.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search