View previous topic :: View next topic |
Author |
Message |
[n00b@localhost] Apprentice
Joined: 30 Aug 2004 Posts: 266 Location: London, UK
|
Posted: Tue Jun 25, 2013 10:38 am Post subject: Creating a shared library: when to link external libs? |
|
|
I am writing a project which performs a Monte Carlo simulation in parallel using MPI. The output of the project is going to be a library that can be linked with other programs.
To create the library I currently have the following GNU make rule:
Code: |
libfoo.so: $(FOO_OBJS)
$(CC) $(LDFLAGS) -shared -o $(@) $(^) $(LOADLIBES) $(LDLIBS)
|
This expands to the following command:
Code: |
gcc -Wl,-soname=libfoo.so.1 -shared -o libfoo.so foo1.o foo2.o foo3.o -lblas -lcblas -lreflapack -lmpi -lpthread -lm
|
This command, without the LDLIBS, however, works as well:
Code: |
gcc -Wl,-soname=libfoo.so.1 -shared -o libfoo.so foo1.o foo2.o foo3.o
|
What is the difference between the two commands? Which one is more "correct"? Does it make any difference to the link command client applications have to use? |
|
Back to top |
|
|
Ahenobarbi Retired Dev
Joined: 02 Apr 2009 Posts: 345 Location: Warsaw, PL
|
Posted: Tue Jun 25, 2013 3:10 pm Post subject: |
|
|
If you call gcc with -shared it will not perform linking, so -lsomething flags will change nothing. |
|
Back to top |
|
|
[n00b@localhost] Apprentice
Joined: 30 Aug 2004 Posts: 266 Location: London, UK
|
Posted: Tue Jun 25, 2013 3:38 pm Post subject: |
|
|
Thanks! |
|
Back to top |
|
|
MustrumR n00b
Joined: 15 Nov 2011 Posts: 71 Location: Right here
|
Posted: Tue Jun 25, 2013 4:18 pm Post subject: |
|
|
WRONG. NEVER forget linking libraries you need.
Consider the following example:
liba.so has add()
libb.so has sub(), implementation details: calls add()
main calls sub()
You think you should only link libb.so to main because you only use libb.so. This is true.
HOWEVER libb.so MUST link liba.so because it NEEDS liba.so.
Link x <=> you use x. |
|
Back to top |
|
|
VoidMage Watchman
Joined: 14 Oct 2006 Posts: 6196
|
Posted: Tue Jun 25, 2013 6:51 pm Post subject: |
|
|
Ahenobarbi wrote: | If you call gcc with -shared it will not perform linking, so -lsomething flags will change nothing. |
Obviously somebody joined the project too late to remember the whole '-Wl,--as-needed' fun. |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21490
|
Posted: Tue Jun 25, 2013 10:12 pm Post subject: |
|
|
Before the program can load, references must be satisfied. If your link line does not embed the appropriate references into the shared library, anyone who wants to load it will need to put the corresponding directives in their link line. That can in turn be complicated by --as-needed removing it from their link line because they really do not need it directly. Link what you need. Your users will be unhappy if you make them link it for you. |
|
Back to top |
|
|
Ahenobarbi Retired Dev
Joined: 02 Apr 2009 Posts: 345 Location: Warsaw, PL
|
Posted: Wed Jun 26, 2013 8:19 am Post subject: |
|
|
I mixed up -shared with -c. |
|
Back to top |
|
|
[n00b@localhost] Apprentice
Joined: 30 Aug 2004 Posts: 266 Location: London, UK
|
Posted: Wed Jun 26, 2013 1:29 pm Post subject: |
|
|
So what should be the link lines for creating the library? What about for an executable that links against the library? |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21490
|
Posted: Wed Jun 26, 2013 10:24 pm Post subject: |
|
|
Link what you need, no more and no less. You can get some help from the linker if you include -Wl,--no-allow-shlib-undefined. In some cases, this may refuse to build a library that is correct. In most cases, if that fails, you are missing a library that you ought to add. |
|
Back to top |
|
|
[n00b@localhost] Apprentice
Joined: 30 Aug 2004 Posts: 266 Location: London, UK
|
Posted: Thu Jun 27, 2013 11:07 am Post subject: |
|
|
So lets take as a real-life example the BLAS library. My library uses BLAS routines. If I create my library and link against the MKL BLAS libraries does this mean that my users need to have the MKL installed to link against my library or would they be able to link against (say) the reference BLAS? If I leave the linking up to the client application then they should be able to link against any BLAS library and not the one I chose to link my library against, yes? |
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21490
|
Posted: Thu Jun 27, 2013 8:54 pm Post subject: |
|
|
That is a poor choice of example because it falls into the rare category of multiple libraries that are supposedly ABI-compatible and therefore interchangeable. When dealing with that class of libraries, it is permissible and sometimes useful not to link any of them, especially if you can assume that any functioning client program would have already linked one of them for its own purposes. For any regular library, there will exist exactly one implementation that can be safely used, and you should specify that implementation on your link line if you use it. |
|
Back to top |
|
|
patrix_neo Guru
Joined: 08 Jan 2004 Posts: 520 Location: The Maldives
|
Posted: Fri Jun 28, 2013 6:56 pm Post subject: |
|
|
Am I the only one seeing this in the original post #1 ?
$(LOADLIBES)
It might not be the cause of the original problem but might cause another headache..I mean LIBES...realy? If it's a customary parameter, I get it though. |
|
Back to top |
|
|
[n00b@localhost] Apprentice
Joined: 30 Aug 2004 Posts: 266 Location: London, UK
|
|
Back to top |
|
|
patrix_neo Guru
Joined: 08 Jan 2004 Posts: 520 Location: The Maldives
|
Posted: Sat Jun 29, 2013 12:05 am Post subject: |
|
|
Mr collage boy...And I stand corrected. |
|
Back to top |
|
|
|