LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-09-2012, 04:06 AM   #1
ratman1
Member
 
Registered: Oct 2009
Location: Perth, Australia
Distribution: Slackware64 14.0
Posts: 44

Rep: Reputation: 0
C programming - questions about libraries needing to be linked in when compiling


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
Code:
-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?

Thanks,
James

Last edited by ratman1; 10-09-2012 at 04:20 AM.
 
Old 10-09-2012, 07:53 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,495

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Quote:
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++.
You shouldn't compile (that is, create .o files from .c files) C programs because it will treat your C source as C++. Linking (that is, create executables from .o files) is fine though. At worst you'll have some unneeded code linked in.

Quote:
Secondly, is it the g++ compiler that is doing the automatic linking of the libraries in Code::Blocks?
You can see what gcc/g++ does by passing the -v option:
Code:
% gcc -v link.o -o link
Using built-in specs.
...
 /usr/lib/gcc/x86_64-linux-gnu/4.7/collect2 ... link.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s ...
Code:
% g++ -v link.o -o link
Using built-in specs.
...
 /usr/lib/gcc/x86_64-linux-gnu/4.7/collect2 ... link.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc ...
So you can see that g++ always links with the math library.
 
1 members found this post helpful.
  


Reply

Tags
c++, codeblocks, geany, libraries, linking


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Beginning C programming- questions about basic use of make and compiling keithostertag Programming 3 03-05-2012 11:48 AM
Changing a binaries linked libraries piercey Linux - General 1 09-12-2007 06:02 PM
Dynamically linked libraries.. vishalbutte Programming 6 04-17-2006 08:54 PM
How to see which shared libraries are linked to a program zsjoska Linux - Newbie 2 11-19-2004 01:45 PM
Linux Dynamicly Linked Libraries sikil_nuru Programming 3 03-16-2004 07:51 AM


All times are GMT -5. The time now is 09:41 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration