Discussion:
'gcc' vs 'c++' for compiling c++ files
(too old to reply)
Arch User via arch-general
2017-09-07 20:41:06 UTC
Permalink
Raw Message
Compiling a sample xxx.cc file using 'c++' command compiles fine whereas 'gcc' command outputs errors. A whole bunch of 'undefined reference to's.
Am i wrong to assume that 'gcc' command would use the file extension and call the appropriate compiler.
Patrick Eigensatz via arch-general
2017-09-07 21:24:58 UTC
Permalink
Raw Message
I guess you are wrong ;)

See `man gcc` where the DESCRIPTION states:

The usual way to run GCC is to run the executable called gcc, or
machine-gcc when cross-compiling, or machine-gcc-version to run a
specific version of GCC.  When you compile C++ programs, you should
invoke GCC as g++ instead.


Have a nice evening!
Patrick
Post by Arch User via arch-general
Compiling a sample xxx.cc file using 'c++' command compiles fine whereas 'gcc' command outputs errors. A whole bunch of 'undefined reference to's.
Am i wrong to assume that 'gcc' command would use the file extension and call the appropriate compiler.
mpan
2017-09-07 22:13:45 UTC
Permalink
Raw Message
While Patrick is right and I agree that you should use the proper
compiler for the given language, it is not true that your assumption
about filenames was wrong.

The `gcc` command is choosing the compiler for the file based on its
suffix¹. Files ending with “.cc” are among these considered to be C++
sources and they should compile fine with `gcc`.

Your description confirms that compilation goes ok. What doesn’t is
linking. Compiled files have no language — they’re already compiled —
and therefore `gcc` has no idea that it should do language-specific
magic, like linking against libstdc++.
____
¹ man gcc → the begining of “Options Controlling the Kind of Output”
Loading...