LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 11-06-2011, 05:49 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
Difference between .a and .o


Hi, I have written some useful functions that I call from the programs that I code. The functions are compile into a .o object file with the followin:
gcc -c func_useful.c

I'd then normally compile it into my programs if I need them with the following:
gcc -o executableName executableSrc.c func_useful.o -lc -lpthread

However, what's the difference between doing it this way versus me using "ar" to compile func_useful.c into func_useful.a?

Both ways require the code to be statically linked in to the executable before run time.
 
Old 11-06-2011, 06:03 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
A *.a library can (usually does) contain multiple *.o objects; when you use it in linkage, only the necessary objects will be added to the executable from the library.
 
1 members found this post helpful.
Old 11-07-2011, 09:26 AM   #3
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Thanks. Essentially I guess they are the same then.
 
Old 11-07-2011, 11:39 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, in your special case they are, but normally a *.a lib does contain multiple objects.
 
Old 11-07-2011, 12:10 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The linker will still treat them differently in a way that often matters (but apparently didn't in your current situation).

A .o file will be linked in even if it doesn't resolve any symbol that was undefined at the point the linker encounters that .o.

A .o wrapped inside a .a will only be linked in if it resolves some undefined symbol.
 
  


Reply



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
LXer: Making a Difference; Selling a Difference LXer Syndicated Linux News 0 09-23-2010 05:00 AM
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
difference between df -k and du -s Prasun1 Linux - General 3 06-22-2005 06:07 PM
What is the difference between WE/WM and DE? eBopBob Linux - Newbie 3 12-12-2004 06:26 AM
What a difference... perry General 3 12-04-2003 05:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration