Gnu Toolchain/Relink

From Devpit
Jump to: navigation, search

Here's the steps to relink an application against a self contained toolchain.

1.) Locate all of the application's .o files. You can also link .a files to pick them all up at once. These will be needed for the relink.

2.) Locate the paths to all of the necessary linked shared-object files, e.g.

/usr/X11R6/lib for libXrender
/opt/gnome/lib for libgtk-x11-2.0

3.) Edit /opt/toolchain/etc/ld.so.conf and add the directories to all of the shared object files to the end of this file. Don't forget lib64 for the 64-bit equivalent libraries if applicable, e.g.

/opt/gnome/lib/
/opt/gnome/lib64/
/usr/X11R6/lib
/usr/X11R6/lib64/

4.) Run the self contained toolchain's ldconfig application to regenerate /opt/toolchain/etc/ld.so.cache, e.g.

sudo /opt/toolchain/sbin/ldconfig

The loader uses /opt/toolchain/etc/ld.so.cache to find the libraries the application was linked against.

5.) Re-link using the self contained toolchain's compiler:

/opt/toolchain/bin/gcc -g -O2 -o <application_name> <list_of_dot_o_files> <list_of_dot_a_files> -L<path_to_libraries> 
-l<one_for_each_library_needed_for_the_link>

e.g.

/opt/toolchain/bin/gcc -g -O2 -o mandelbrot callbacks.o  interface.o  main.o  quadmand.o  support.o mandel_internals.a \
-L/usr/X11R6/lib -L/usr/X11R6/lib64 -L/opt/gnome/lib -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 \
-lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -ldl -lglib-2.0 -lfreetype -lfontconfig \
-lXrender -lX11 -lXext -lpng12 -lz -lglitz -lm -lstdc++ -lpthread -lgthread-2.0

6.) If ld gives an error like the following then you're missing the path to that library in the link stage. Add it with -L<path to library>, e.g.

/opt/toochain/bin/ld: cannot find -lgtk-x11-2.0

Add -L/opt/gnome/lib/ to the gnome compilation line. You need to tell the linker where to find all of the libraries.

7.) When running the re-linked application if you get an error like the following:

./mandelbrot: error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory

you need to add the path to the library in question to /opt/toolchain/etc/ld.so.conf and rerun /opt/toolchain/sbin/ldconfig. The self contained toolchain's loader needs to know where to find the libraries and uses the generated /opt/toolchain/etc/ld.so.cache to find them.

8.) You can verify that the self contained toolchain's libraries were picked up by running the application prefaced with LD_DEBUG=libs, e.g.

LD_DEBUG=all ./mandelbrot