Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Creating a shared library: when to link external libs?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
[n00b@localhost]
Apprentice
Apprentice


Joined: 30 Aug 2004
Posts: 211
Location: London, UK

PostPosted: Tue Jun 25, 2013 10:38 am    Post subject: Creating a shared library: when to link external libs? Reply with quote

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
View user's profile Send private message
Ahenobarbi
Developer
Developer


Joined: 02 Apr 2009
Posts: 345
Location: Warsaw, PL

PostPosted: Tue Jun 25, 2013 3:10 pm    Post subject: Reply with quote

If you call gcc with -shared it will not perform linking, so -lsomething flags will change nothing.
Back to top
View user's profile Send private message
[n00b@localhost]
Apprentice
Apprentice


Joined: 30 Aug 2004
Posts: 211
Location: London, UK

PostPosted: Tue Jun 25, 2013 3:38 pm    Post subject: Reply with quote

Thanks!
Back to top
View user's profile Send private message
MustrumR
n00b
n00b


Joined: 15 Nov 2011
Posts: 45
Location: Right here

PostPosted: Tue Jun 25, 2013 4:18 pm    Post subject: Reply with quote

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
View user's profile Send private message
VoidMage
Watchman
Watchman


Joined: 14 Oct 2006
Posts: 5270

PostPosted: Tue Jun 25, 2013 6:51 pm    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Watchman
Watchman


Joined: 06 Mar 2007
Posts: 8595

PostPosted: Tue Jun 25, 2013 10:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ahenobarbi
Developer
Developer


Joined: 02 Apr 2009
Posts: 345
Location: Warsaw, PL

PostPosted: Wed Jun 26, 2013 8:19 am    Post subject: Reply with quote

:oops:

I mixed up -shared with -c.
Back to top
View user's profile Send private message
[n00b@localhost]
Apprentice
Apprentice


Joined: 30 Aug 2004
Posts: 211
Location: London, UK

PostPosted: Wed Jun 26, 2013 1:29 pm    Post subject: Reply with quote

So what should be the link lines for creating the library? What about for an executable that links against the library?
Back to top
View user's profile Send private message
Hu
Watchman
Watchman


Joined: 06 Mar 2007
Posts: 8595

PostPosted: Wed Jun 26, 2013 10:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
[n00b@localhost]
Apprentice
Apprentice


Joined: 30 Aug 2004
Posts: 211
Location: London, UK

PostPosted: Thu Jun 27, 2013 11:07 am    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Watchman
Watchman


Joined: 06 Mar 2007
Posts: 8595

PostPosted: Thu Jun 27, 2013 8:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
patrix_neo
Guru
Guru


Joined: 08 Jan 2004
Posts: 372
Location: Svedala

PostPosted: Fri Jun 28, 2013 6:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
[n00b@localhost]
Apprentice
Apprentice


Joined: 30 Aug 2004
Posts: 211
Location: London, UK

PostPosted: Fri Jun 28, 2013 8:04 pm    Post subject: Reply with quote

http://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html

GNU Make Manual wrote:
Linking a single object file
n is made automatically from n.o by running the linker (usually called ld) via the C compiler. The precise recipe used is ‘$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)’.
Back to top
View user's profile Send private message
patrix_neo
Guru
Guru


Joined: 08 Jan 2004
Posts: 372
Location: Svedala

PostPosted: Sat Jun 29, 2013 12:05 am    Post subject: Reply with quote

[n00b@localhost] wrote:
http://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html

GNU Make Manual wrote:
Linking a single object file
n is made automatically from n.o by running the linker (usually called ld) via the C compiler. The precise recipe used is ‘$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)’.


Mr collage boy...And I stand corrected.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum