It is currently Fri Jun 22, 2018 1:01 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Linking to Static Libraries
PostPosted: Tue Oct 09, 2012 10:06 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3252
Location: New Zealand
When you try to link to a static library, you will often get a lot of undefined references

Methodology to resolve the problem

1) Add an Import to the library your linking to
Code:
ImportC "libbar.a" : EndImport


2) Add any Imports to know dependencies
Code:
ImportC "libbar.a" : EndImport
ImportC "-lstdc++" : EndImport   ;<------


3) Add Imports directly below the first import statement for a library that's reported an error eg undefined refference to foo ... first appears in libfoo
Code:
ImportC "libbar.a" : EndImport
ImportC "-libfoo" : EndImport   ; <------
ImportC "-lstdc++" : EndImport



4) Add the functions to the library your linking to
Code:
ImportC "libbar.a" : EndImport
ImportC "-libfoo" : EndImport
ImportC "-lstdc++" : EndImport 
ImportC "libbar.a"   ;<------
   foo()
   bar()
EndImport


Additionally some libraries may have multiple definitions and may still have unresolved symbols

Add these to the top
Code:
ImportC "-Wl,--allow-multiple-definition" : EndImport   
ImportC "-Wl,--unresolved-symbols=ignore-in-object-files" : EndImport  ;use as a last resort 

ImportC "-libfoo" : EndImport
ImportC "-lstdc++" : EndImport 
ImportC "libbar.a"
   foo()
   bar()
EndImport


Top
 Profile  
Reply with quote  
 Post subject: Re: Linking to Static Libraries
PostPosted: Tue Oct 09, 2012 10:25 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3629
Location: Italy
Interesting, so are these linker flags ?

Quote:
ImportC "-Wl,--allow-multiple-definition" : EndImport
ImportC "-Wl,--unresolved-symbols=ignore-in-object-files" : EndImport ;use as a last resort


Does this work under windows too ? The linker flag thing I mean.

Because this is definitely not documented, could be useful!

Thanks.

_________________
[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
 Post subject: Re: Linking to Static Libraries
PostPosted: Tue Oct 09, 2012 10:43 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3252
Location: New Zealand
Yes they're linker flags, the -Wl option specifies to gcc to pass the flags to the a linker

I don't know if you can do something similar on windows to set additional flags for polink
I've never encountered a situation where I've had to try.

_________________
Got winter blues?
Enjoy a Caravan Trip into, "The Land of Grey and Pink", wine and punk weed optional!
https://www.youtube.com/watch?v=D5iX9YhCCp8


Top
 Profile  
Reply with quote  
 Post subject: Re: Linking to Static Libraries
PostPosted: Tue Nov 19, 2013 4:10 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Sep 06, 2006 6:59 am
Posts: 195
Those linker flags works fine even with shared libs and OpenLibrary() stuff. Seems even helpful if you have a lib in a non-standard-path:

Code:
ImportC "-Wl,-rpath=/opt/lib" : EndImport


This just solved my issue with GnutTLS 3.2.6. While older versions placed at /opt/lib just worked without issue via OpenLibrary() the new version compiled with the same flags refused to work without this for unknown reason (I tried to avoid calling some "export LD_LIBRARY_PATH=/opt/lib" or adding a further path into "/etc/ld.so.conf")


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye