LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Compiling dependencies (https://www.linuxquestions.org/questions/linux-software-2/compiling-dependencies-4175502065/)

Hughey 04-17-2014 10:03 AM

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

Hughey 04-17-2014 11:39 AM

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.

Hughey 04-17-2014 11:53 AM

...

John VV 04-17-2014 10:36 PM

if you do not actually post the code and the REAL errors
Then there is not much anyone can do to help

pan64 04-18-2014 12:20 AM

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).

Hughey 04-18-2014 04:25 PM

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

jpollard 04-18-2014 04:27 PM

Quote:

Originally Posted by Hughey (Post 5154584)
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.

pan64 04-19-2014 04:12 AM

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)

sundialsvcs 04-19-2014 08:06 AM

Makefiles usually handle this. See man make. And automake.


All times are GMT -5. The time now is 12:33 AM.