Re: How to static link a program?



On Fri, Feb 04, 2005 at 07:48:14PM -0800, James Frye wrote:
...
> 
> So how can I statically link my program (or at least the GtkGl library
> part of it) so that the executable will run on any system?  There doesn't
> seem to be a make or configure option to even make static libs...

huh.  as far as I can tell, the standard configure script created by
glade has no options to specify static linking.  pkg-config doesn't seem
to support static libs either.  I don't know why that is.

Anyway...  If your make process is pretty simple, it's not too hard to
do all your static linking by hand.  And, once you can do it by hand,
it's not all that difficult to modify your Makefiles so that they choose
static libraries instead.  All you have to do is choose the libs you
want to be linked statically, remove the dynamic versions from your link
line and then add the static version as if they're just additional
object files.  for example:

ben elsa:~/Projects/hpntpos/src$ rm hpntpos
ben elsa:~/Projects/hpntpos/src$ make
gcc  -g -O2  -o hpntpos  main.o support.o interface.o callbacks.o page_lockedscreen.o page_shiftstart.o page_shiftend.o page_switchboard.o page_cashdrawermngmnt.o page_transactions.o hpnt_log.o access_checking.o signals.o state.o hpnt_db.o hpnt_options.o timer_queue.o window_enter_number_dlg.o window_keyboard_dlg.o hpntgroupedtoggle.o -Wl,--export-dynamic -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0 -lpangox-1.0 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -ldl -lglib-2.0 -lmysqlclient -lcrypt -lnsl -lm -lz -lglib-2.0

# so... hpntpos is created here from a bunch of object files and is linked
# to a bunch of dynamic libs.  to get rid of a few dymamic libs, say...
# libgobject-2.0 and libgmodule-2.0 I can find the static versions, 
# /usr/lib/libgobject-2.0.a and /usr/lib/libgmodule-2.0.a and create a
# new link line on the command line.

ben elsa:~/Projects/hpntpos/src$ rm hpntpos
ben elsa:~/Projects/hpntpos/src$ gcc  -g -O2  -o hpntpos  /usr/lib/libgobject-2.0.a /usr/lib/libgmodule-2.0.a  main.o support.o interface.o callbacks.o page_lockedscreen.o page_shiftstart.o page_shiftend.o page_switchboard.o page_cashdrawermngmnt.o page_transactions.o hpnt_log.o access_checking.o signals.o state.o hpnt_db.o hpnt_options.o timer_queue.o window_enter_number_dlg.o window_keyboard_dlg.o hpntgroupedtoggle.o -Wl,--export-dynamic -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0 -lpangox-1.0 -lpango-1.0 -ldl -lglib-2.0 -lmysqlclient -lcrypt -lnsl -lm -lz -lglib-2.0


So, it's not automatic, and could end up creating a fair amount of work,
depending on how complex your build process is.  But if you only have to
do it once then it's not a big deal.  And...  of course, after you do it
once you can add your new command line in your Makefile.  like...

# ---------------------------------------------------------------------
static_LIBS = /usr/lib/libgobject-2.0.a /usr/lib/libgmodule-2.0.a

modified_DYNLIBS = -Wl,--export-dynamic -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0 -lpangox-1.0 -lpango-1.0 -ldl -lglib-2.0 -lmysqlclient -lcrypt -lnsl -lm -lz -lglib-2.0

static: $(hpntpos_OBJECTS)
        gcc  -g -O2  -o hpntpos  $(static_LIBS) $(hpntpos_OBJECTS) $(modified_DYNLIBS)
# ---------------------------------------------------------------------



it's just a quick hack, but if it works and you don't have to ever touch
it again then ...  why not?

- Ben




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]