library that links to libraries
I created a static library that itself links to other libraries that it uses. Why is it that I need to link to those other libraries from any application that I try to build using my custom library?
Is there a way around this behavior? One of the main reasons I wanted to use a library is to avoid linking in the applications, except to the library I created.
[370 byte] By [
aewarnick] at [2007-11-20 10:50:18]

# 1 Re: library that links to libraries
How does it link to the other libraries it uses?
# 2 Re: library that links to libraries
I guess if u need to link other libraries to custom library statically. then other application will not require linking with other libraries while linking to ur custom library..sorry if I m wrong in understanding ur issue.
# 3 Re: library that links to libraries
I created a static library that itself links to other libraries that it uses.You don't mention, if you link statically or dynamically to the other libraries. If the latter, this obviously won't work. If your library is itself dynamic and you link in static libraries, you might run into trouble, depending how the static libraries are compiled (i.e. if they contain position independent code or not). If your library is static and you link in the other libraries statically this should work as you expect.
# 4 Re: library that links to libraries
By dynamic, I assume you mean to shared objects. I'm linking in the linking options of CodeBlocks to .a files, which I thought were always static libraries. I think the gcc option that is being used is -L. If this is all correct, I don't know why it's not working.
# 5 Re: library that links to libraries
-L is the directory in which the libraries are to be found. -l is used to link in the libraries themselves.
Note that when you use -l you omit the "lib" at the start of the name and the ".a" or ".so" at the end, and the default I think if both exist is to link in the .so
# 6 Re: library that links to libraries
By dynamic, I assume you mean to shared objects. I'm linking in the linking options of CodeBlocks to .a files, which I thought were always static libraries. I think the gcc option that is being used is -L. If this is all correct, I don't know why it's not working.-L just tells gcc where to look for libraries in addition to the usual places (i.e. it will always look in /lib and /usr/lib). -l tells gcc to link a library but it will first try to find a shared library and only if that is not found will use the static library. It might be safer for you to not use the -L/-l options but simply pass the archive file name (the static library) as an additional object to the linker.
# 7 Re: library that links to libraries
I thought (I could be wrong) that you cannot link static libraries to static libraries. All the linker does is confirm that it can find all the proper symbols, but it doesn't actually link the libraries together into one big library. I think that you still need to add all the static libs to your executable link line.
Viggy
# 8 Re: library that links to libraries
I thought (I could be wrong) that you cannot link static libraries to static libraries. All the linker does is confirm that it can find all the proper symbols, but it doesn't actually link the libraries together into one big library. I think that you still need to add all the static libs to your executable link line.
Viggy
That's how I understand it as well.
When you create a static library the linker is not even run. Instead of the linker the archiver ( ar ) is run and that just bundles all the .o files into a big .a file.
Guess you could manually extract the .o files from that extra libraries and then combine them into your new static library. ( I' never tried that )
Kurt
ZuK at 2007-11-9 1:31:42 >

# 9 Re: library that links to libraries
I thought (I could be wrong) that you cannot link static libraries to static libraries. All the linker does is confirm that it can find all the proper symbols, but it doesn't actually link the libraries together into one big library.OK, true.
I thought I had done something like that years back, but now I checked and found it was a bit hacky.
Here is how it went:
$ # compile a.c and b.c
$ gcc -c a.c
$ gcc -c b.c
$ # Create an static library from a.o and b.o
$ ar rcs libab.a a.o b.o
$ # Cleanup
$ rm a.o b.o
$ # List the contents of the library
$ nm libab.a
a.o:
00000000 T a
b.o:
00000000 T b
$ # compile c.c
$ gcc -c c.c
$ # Create a static library from c.o and the archives in libab.a
$ ar rcs libabc.a c.o `ar xv libab.a | sed -e"s/x -//"`
$ # List the contents of the new library
$ nm libabc.a
c.o:
00000000 T c
a.o:
00000000 T a
b.o:
00000000 T b
# 10 Re: library that links to libraries
I'd have to do that with every .o file, wouldn't I? Would this make my executable huge? Or is ar smart enouph not to link things that have already been linked?
# 11 Re: library that links to libraries
That's it. I remember now. One of the reasons for not creating one big library from a bunch of little static libraries is this. What if you have two libraries that link in the same smaller library? Then, what if you link those two libraries to an executable? Which code should the linker use for the common library?
Kinda similar to the C++ diamond inheritance issue.
Viggy
# 12 Re: library that links to libraries
In light of these depressing issues, I may just create one dynamic shared library.
I've heard that you can't inline functions that way, but if in the headers a function is declared inline, it should be compiled directly into the executable, right?
The other issue I can see would mean a ton of work. That is, declaring all functions, other than inline ones, extern. Do I need to do this?
# 13 Re: library that links to libraries
Well, you do need to export them from the DLL. Either using a DEF file, or using '__declspec(dllexport)'. Well, on Windows anyway (in *NIX, everything is "exported").
Viggy
# 14 Re: library that links to libraries
There was an option in CodeBlocks to create a .a along with the .dll. I thought it would allow me to not change my source code at all, but I compile linking to that library and it does not help.
Looks like I'll have to keep compiling straight from source all the time like I have been. I thought there was a better way.