I am learning C (still very much a beginner) on Linux using the GCC compiler. I have noticed that some libraries, such as the library used with the math.h
header, need to be linked in manually when included. I have been linking in the libraries using various flags of the form -l[library-name]
, such as -lm
for the above-mentioned math library.
However, after switching from the command line and/or Geany
to Code::Blocks
, I noticed that Code::Blocks
uses g++
to compile the programs instead of the gcc
that I am used to (even though the project is definitely specified as C). Also, Code::Blocks
does not require the libraries to be manually linked in when compiling – libraries such as the math library just work.
I have two questions:
Firstly, is it "bad" to compile C programs with the g++
compiler? So far it seems to work, but after all, C++ is not C and I am quite sure that the g++
compiler is meant for C++.
Secondly, is it the g++
compiler that is doing the automatic linking of the libraries in Code::Blocks
?
Best Answer
Both gcc and g++ are frontends to the GNU compiler collection. You should use the former for compiling and linking C code, and the latter for performing the same actions on C++ code. One of the strongest arguments for maintaining the distinction is that C is not a subset of C++.
If you link using g++ it will automatically link in the C++ standard library. Since the C standard library is part of the C++ standard library, the math library is included as well. This is why you do not need to link in the math library manually.