linking against static libs?[solved-maybe]

Linux specific forum
User avatar
idle
Always Here
Always Here
Posts: 5895
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

linking against static libs?[solved-maybe]

Post by idle »

I'm having problems linking against a static lib and can't figure out how to resolve the issue

the lib has a package config listing the dependencies as
Requires.private: gl x11 xrandr xxf86vm
Libs: -L${libdir} -lglfw
Libs.private: -lrt -lm
So I've Imported these but still I get a linker error
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crtn.o/usr/bin/ld: /usr/local/lib/libglfw.a(x11_clipboard.c.o): undefined reference to symbol 'XConvertSelection'
/usr/bin/ld: note: 'XConvertSelection' is defined in DSO /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libX11.so so try adding it to the linker command line
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libX11.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
Even though I've linked to libX11.so?
The lib works fine from gcc so I don't see what the problem is.

The trace, clearly shows that the lib is loaded
pbcompiler ~/Desktop/glfw3/libglfw3.pbi

******************************************
PureBasic 4.61 (Linux - x86)
******************************************

Loading external modules...
Starting compilation...
838 lines processed.
Creating the executable.
Error: Linker
/usr/bin/ld: mode elf_i386
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o
/usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o
purebasic.o
(Array.a)Array.o
(Array.a)CopyArray.o
(Array.a)FreeArray.o
/lib/i386-linux-gnu/libc.so.6
(/usr/lib/i386-linux-gnu/libc_nonshared.a)elf-init.oS
/lib/i386-linux-gnu/ld-linux.so.2
-lXxf86vm (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libXxf86vm.so)
-lXrandr (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libXrandr.so)
-lX11 (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libX11.so)
-lGL (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libGL.so)
-lGLU (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libGLU.so)
-lrt (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/librt.so)
-lm (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libm.so)
(/usr/local/lib/libglfw.a)fullscreen.c.o
(/usr/local/lib/libglfw.a)init.c.o
(/usr/local/lib/libglfw.a)input.c.o
(/usr/local/lib/libglfw.a)opengl.c.o
(/usr/local/lib/libglfw.a)time.c.o
(/usr/local/lib/libglfw.a)window.c.o
(/usr/local/lib/libglfw.a)x11_fullscreen.c.o
(/usr/local/lib/libglfw.a)x11_init.c.o
(/usr/local/lib/libglfw.a)x11_input.c.o
(/usr/local/lib/libglfw.a)x11_joystick.c.o
(/usr/local/lib/libglfw.a)x11_opengl.c.o
(/usr/local/lib/libglfw.a)x11_time.c.o
(/usr/local/lib/libglfw.a)x11_window.c.o
(/usr/local/lib/libglfw.a)error.c.o
(/usr/local/lib/libglfw.a)x11_clipboard.c.o
(/usr/local/lib/libglfw.a)x11_gamma.c.o
(/usr/local/lib/libglfw.a)x11_keysym2unicode.c.o
(/home/andrew/purebasic46/purebasic/compilers/systembase.a)CopyStructure.o
(/home/andrew/purebasic46/purebasic/compilers/systembase.a)FreeStructureStrings.o
(/home/andrew/purebasic46/purebasic/compilers/systembase.a)InitDynamicStructure.o
(/home/andrew/purebasic46/purebasic/compilers/systembase.a)IsDynamicStructure.o
(/home/andrew/purebasic46/purebasic/compilers/systembase.a)DynamicFunctions.o
(/home/andrew/purebasic46/purebasic/compilers/systembase.a)System.o
(/home/andrew/purebasic46/purebasic/compilers/stringmanager.a)FastAllocateString.o
(/home/andrew/purebasic46/purebasic/compilers/stringmanager.a)FastStringCopy.o
(/home/andrew/purebasic46/purebasic/compilers/stringmanager.a)InitString.o
(/home/andrew/purebasic46/purebasic/compilers/stringmanager.a)FreeString_ASM.o
-ldl (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libdl.so)
-lgcc_s (/usr/lib/gcc/i686-linux-gnu/4.6/libgcc_s.so)
/lib/i386-linux-gnu/libc.so.6
(/usr/lib/i386-linux-gnu/libc_nonshared.a)atexit.oS
/lib/i386-linux-gnu/ld-linux.so.2
-lgcc_s (/usr/lib/gcc/i686-linux-gnu/4.6/libgcc_s.so)
/usr/lib/gcc/i686-linux-gnu/4.6/crtend.o
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crtn.o/usr/bin/ld: /usr/local/lib/libglfw.a(x11_clipboard.c.o): undefined reference to symbol 'XConvertSelection'
/usr/bin/ld: note: 'XConvertSelection' is defined in DSO /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libX11.so so try adding it to the linker command line
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libX11.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
So any ideas how to get around the problem?
My attempts to get it to link in PB

Code: Select all


ImportC "-Wl,--trace" : EndImport  :print the trace 
ImportC "-Wl,--warn-unresolved-symbols" : EndImport  ;turn errors to warnings 
ImportC "-Wl,--allow-multiple-definition" : EndImport  ; if multiple defs takes the first one
ImportC "-Wl,--export-dynamic" : EndImport ;export all symbols 
ImportC "-Wl,--start-group" : EndImport  ; supposed to loop around resolving symbols 
ImportC "-lXxf86vm" 
ImportC "-lXrandr" 
ImportC "-lX11" 
ImportC "-lGL" 
ImportC "-lGLU" 
ImportC "-lrt"
ImportC "-lm" 
ImportC "/usr/local/lib/libglfw.a" 
ImportC "-Wl,--end-group" : EndImport   

I also tried to use a linker options file but it didn't appear to work or accept ld commands
and I don't see any option to pass ld commands on the command line!

I'd really like to know how we can do this in PB.
Last edited by idle on Mon Jun 04, 2012 9:50 pm, edited 1 time in total.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
idle
Always Here
Always Here
Posts: 5895
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: how to link against static libs?

Post by idle »

After fluffing about I eventually got it to link and think I've found a fool proof methodology
to work through linking errors

Add a import reference to the lib your linking to as the first import
Add imports for the know dependencies of the lib
Insert any lib imports reported by a linker error as the second entry
Add your libs Import functions last

Example

Code: Select all

 
ImportC "/usr/local/lib/libglfw.a" : EndImport  ;Add import reference to the lib your linking to 1st   
ImportC "-lgtk-x11-2.0" : EndImport  ;insert any libs reported by the linker in this position  
ImportC "-lrt" : EndImport   ;add known dependencies 
ImportC "-lm" : EndImport
ImportC "-lGL" : EndImport  
ImportC "-lGLU" : EndImport  
ImportC "-lXrandr" : EndImport  
ImportC "-lXxf86vm" : EndImport  
ImportC "-lX11" : EndImport      

;then declare the imports  
ImportC "/usr/local/lib/libglfw.a"
 glfwInit();
 glfwTerminate();
 ...
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: how to link against static libs?

Post by luis »

idle wrote: Add a import reference to the lib your linking to as the first import
Thank you idle, this helped me to solve a similar linking problem.

Another example of how replying to yourself when you find a solution to a problem you had can help someone else later :wink:
"Have you tried turning it off and on again ?"
User avatar
idle
Always Here
Always Here
Posts: 5895
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: linking against static libs?[solved-maybe]

Post by idle »

your welcome luis.
Linking to static libs can be very frustrating
Windows 11, Manjaro, Raspberry Pi OS
Image
benmalartre
User
User
Posts: 27
Joined: Thu Mar 14, 2013 11:24 am
Location: paris
Contact:

Re: linking against static libs?[solved-maybe]

Post by benmalartre »

Hello, I'm encoutering linkers problem as seen in this post with PB5.4/GLFW3 static lib/Linux(ubuntu)
My code compile/run fine on PB5.3

Anyone's having the same problem?

http://www.forums.purebasic.com/english ... 8021365a29
Post Reply