LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-17-2014, 10:03 AM   #1
Hughey
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Rep: Reputation: Disabled
Post Compiling dependencies


I am writing a static library (say called AAA) which has a dependency on another proprietary library (say called BBB)

I build AAA and link to BBB:
# gcc -c -o AAA.o AAA.c -l BBB
# ar rcs AAA.a AAA.o

AAA.a is produced and there are no errors


When I build a program which needs to use AAA, I compile it and link to AAA.

# gcc -Wall -g -c demo.c -o demo.o
# gcc -g -o demo demo.o -L. -l AAA

However I get a number of errors complaining about :

In function `SomeAAAfunctionname':
undefined reference to `somefunctionBBBname'

What am I missing?
What is the correct way to build a .c program which has a dependency on a library, when that library also has a dependency on another library.

Thanks
 
Old 04-17-2014, 11:39 AM   #2
Hughey
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
OK I have just come across this link, which, if I understand it correctly says that it is not possible to link one static library to another.

http://stackoverflow.com/questions/2...atic-libraries

That is a huge oversight and makes producing layered, encapsulated components impossible using static libraries.

Come back Windows all is forgiven.
 
Old 04-17-2014, 11:53 AM   #3
Hughey
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
...
 
Old 04-17-2014, 10:36 PM   #4
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,627

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
if you do not actually post the code and the REAL errors
Then there is not much anyone can do to help
 
Old 04-18-2014, 12:20 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
in general you do not need to link libraries to each other but to the application.
from the other hand:
Code:
# gcc -c -o AAA.o AAA.c -l BBB
you want to compile AAA.c and here you may use -I to specify include dirs but -l BBB will be ignored (gcc -c means there will be no linking at all).
 
Old 04-18-2014, 04:25 PM   #6
Hughey
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Real output follows :

-- building AAA library which depends on library BBB

root@mtu:~/bcm2835-1.36/examples/blink# gcc -c -o AAA.o AAA.c -l BBB
root@mtu:~/bcm2835-1.36/examples/blink# ar rcs libAAA.a AAA.o

-- building program which depends on library AAA (shows undefined references to entrypoints in BBB)

root@mtu:~/bcm2835-1.36/examples/blink# gcc -Wall -g -c demo.c -o demo.o
root@mtu:~/bcm2835-1.36/examples/blink# gcc -g -o demo demo.o -L. -l AAA
./libAAA.a( AAA.o): In function `initGPIO':
AAA.c: (.text+0x1a4): undefined reference to `BBB_init'
./libAAA.a( AAA.o): In function `delayms':
AAA.c: (.text+0x1d8): undefined reference to `BBB_delay'


Note that I do not want to have to relink the dependencies of a library I am using whenever I am building code which depends on that library. It it possible to do this - i.e. is it possible to build AAA so that the demo.c does not need to know about BBB.

Thanks
 
Old 04-18-2014, 04:27 PM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by Hughey View Post
OK I have just come across this link, which, if I understand it correctly says that it is not possible to link one static library to another.

http://stackoverflow.com/questions/2...atic-libraries

That is a huge oversight and makes producing layered, encapsulated components impossible using static libraries.

Come back Windows all is forgiven.
If you read your link, it is also not possible on Windows.

What you are talking about is combining object files into a new library. That is a librarian function, not a linking function.

Also note: You really need to be careful when you do this - make sure the licensing for all libraries are compatible. Otherwise your resulting library will not be distributable.

Now if this is strictly for local operation, the trivial way is to use the librarian to just add new modules to an existing library.

It doesn't matter if there are extra modules there - when an executable is created the unused modules are left out.
 
Old 04-19-2014, 04:12 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
once more:
gcc -c means compiling, linking will not occure (using -c). Therefore executing this command: gcc -c -o AAA.o AAA.c -l BBB means only compiling, -l BBB will be ignored, it will not be used at all. It is not possible to link static (or dynamic) library that way.
Furthermore using that kind of syntax will not define any kind of dependency. ar rcs libAAA.a AAA.o will produce libAAA.a, but this lib will not contain any information related to BBB.

If you want to have a static library containing both AAA and BBB you need to do it by yourself. you need combine the content of both: copy libBBB.a to libCCC.a and ar r libCCC.a AAA.o for example will do the job. Afterward you need to link to CCC (or use both AAA and BBB)
 
Old 04-19-2014, 08:06 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Makefiles usually handle this. See man make. And automake.
 
  


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
gnome-sharp not compiling despite all dependencies being installed. spoovy Slackware 1 11-17-2010 02:57 PM
[SOLVED] Compiling from source - installed dependencies not found khinch Slackware 3 10-15-2010 04:16 AM
Compiling libusb c++ program to run without it's dependencies linuxmandrake Programming 10 08-06-2010 08:09 PM
Problems compiling gtk+ (and dependencies) on Solaris 8 cbrolle Solaris / OpenSolaris 7 09-13-2006 03:20 PM
Compiling Source & Dependencies arpanet1969 Linux - Newbie 7 12-30-2004 07:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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