Vala: compiling without linking; linking later

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
tsura
Posts: 3
Joined: Tue 01 Dec 2009, 08:25

Vala: compiling without linking; linking later

#1 Post by tsura »

Is anyone here an expert on linking? I'm pretty new to it (except as it is done automatically).

I'm making a project planned to be GNU GPL licensed, however there are several libraries that are GPL that I want to use. It'll be a lot of hassle for me if people who use my program have to install the libraries separately. The auto-linking at compile time doesn't make them a part of the binary (i.e. it links them dynamically).

So, since I want to make it GPL and release the source, it shouldn't a problem to make it all one binary file (to link them statically). So, how do I do this? I figure I need to use ld or something, but I really have no idea how, especially as valac -c fileName still produces binaries instead of object files.

Also, I don't mind using dynamic linking if I can change the path it searches for the libraries to my program's folder, or a subfolder.

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#2 Post by amigo »

LDFLAGS=-static usually will do the trick if the static libs are indeed present. If when finished, the 'file' utility identifies it as 'satically linked', them you can be sure that everything was linked statically. if even one static lib is not present, then the shard version will be linked in and the finished object will be listed as 'shared object'

To make the linker look somewhere besides the standard locations, use LD_LIBRARY_PATH.
You can create a wrapper script which will start your program(or lib) by renaming the real binary to something like 'proggie-bin'. Then, create a shell wrapper name 'proggie' with something like this in it:

Code: Select all

#!/bin/sh
LD_LIBRARY_PATH=/path (to the libs you want to be used)
# and if, needed:
PATH=/path/to/proggie-bin:$PATH
export LD_LIBRARY_PATH PATH
As I said, this trick will even work for using libraries instead of programs, since dynamically-liinked libraries get 'run' just like programs -by being executed by /lib/ld-linux.so.2

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#3 Post by technosaurus »

There are a couple things you can do:

for something that looks like this:
gcc -c -o myprogram myprogram.c -lz -L/tmp/myprogram_install/lib -lmystaticlib -lmysharedlib -liberty .....

you can do something like this
gcc -c -o myprogram.o myprogram.c

and later do this
gcc -o myprogram myprogram.o /lib/libz.so /tmp/myprogram_install/lib/mystaticlib.a /usr/lib/mysharedlib.so -liberty .....

this will link in only the libz in /lib
and link in static library mystaticlib.a from /tmp/myprogram_install/lib
and dynamically link shared library /usr/lib/mysharedlib.so (but only if it is in /usr/lib)
and try to find libiberty.so in /lib /usr/lib /usr/local/lib and whatever else is in the library search path (you can add -L/<whatever>/lib to specify additional paths)... if it doesn't find libiberty.so then it will try to find and statically link in libiberty.a

the problem with waiting until install to do this is that the user would need gcc, but using combinations of the above methods it is possible to change things up to do a partial static compile (useful if you do not have all of the *.a files)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply