2

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).

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Anon Anon
  • 35
  • 6

2 Answers2

3

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

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

nnzzll
  • 328
  • 1
  • 9
  • Thanks, I verified your answer was right from one of the links provided in the answer you mentioned: how g++ is invoked (https://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html) 'On many systems, g++ is also installed with the name c++' – Anon Anon Oct 13 '22 at 06:40
1

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.

Serguei
  • 236
  • 3
  • 12