Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
1  
I guess that Code:Blocks sets some environment variables to search paths for the library in it's build environment or adds this information when it calls the compiler. You could get similar results on the commandline. Most likely there are some setting options in Code:Blocks where you can define such details about your build environment. If you put similar information in .bashrc (or some other ini scripts), you would get the same results. Maybe set Code:Blocks compile output to verbose (if that's possible) to see what exactly it does. –  thorsten müller Oct 9 '12 at 9:23
    
I suppose this link will give you some help that why while compiling with g++ we need not manually link maths library : stackoverflow.com/questions/1033898/… –  user136307 Jun 11 at 15:28
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

Firstly, is it "bad" to compile C programs with the g++ compiler?

Define "bad".

There are a few things which may cause a valid C program to compile in C++ with a different semantic, if my experience is pertinent, you have more chances to find UB handled differently when changing the version of your C compiler than to meet them.

A C++ compiler will not compile some valid C programs, so compiling C with a C++ compiler will restrict you to their common subset, which is a stricter version of C. Restricting yourself to that C dialect is not not idiomatic (the major difference is the need to cast void* when another pointer type is needed) do so but quite a few projects started to use C++ in that way. They where using a C++ compiler to get a more type-safe version of C and then decided to allow the use of the additional features, one by one, as they deemed them pertinent. (The latest and more public example is gcc itself. GCC started to be written in K&R C, then mandated an ANSI C compiler, then was restricted to the common subset of ANSI C and C++, and now they are starting to use C++, another quite public example is that the examples in the second edition of K&R C were compiled with a C++ compiler -- but I think C++ was tightened since as it seems to me that some examples won't compile with a current C++ compiler).

Note that if you only use C++ compilers, you'll quickly introduce C++ dependencies in it.

Note also that of the new things in C99 and C11 are more subject to be different in C++.

Secondly, is it the g++ compiler that is doing the automatic linking of the libraries in Code::Blocks?

When used to link, a compiler always link a default set of libraries. That set for gcc doesn't included libm, it is included in the default set for g++. G++ will not auto-detect additional libraries if they are needed (AFAIK neither will Code::Blocks), libm is a very particular case (some will argue that the fact that gcc doesn't link it by default is a bug in gcc).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.