Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-16-2006, 10:32 AM   #1
Registered: Jul 2004
Location: Herent, Belgium, Europe, Earth
Distribution: Ubuntu 7.04
Posts: 102

Rep: Reputation: 15
[gcc] how to statically link my program?


I made a program with the wxWidgets library. But I don't know how to statically link it to that library?

What I want to achieve is that all the wxWidgets library code is inside the executable (such as a .lib in Windows). All the other libraries can be dynamically linked. This is because I know if you run my program you will have all the X-libraries on your system... But I can't assume you have the wx-ones.

What I have done so far (and works great for dynamically linking):



CFLAGS=-c -Wall -I/usr/lib/wx/include/gtk2-unicode-release-static-2.7 -I/usr/include/wx-2.7 -O2 -D__WXGTK__ -D_UNICODE -fPIC

LDFLAGS=-pthread -L/usr/lib -s -lwx_gtk2u_adv-2.7 -lwx_gtk2u_core-2.7 -lwx_baseu_net-2.7 -lwx_baseu-2.7

SOURCES=$(wildcard *.cpp)
OBJECTS := $(patsubst %.cpp,$(BUILDDIR)/%.o,$(SOURCES))


	if [ ! -d $(BUILDDIR) ]; then mkdir -p $(BUILDDIR); fi

	$(CC) $(LDFLAGS) $(OBJECTS) -o $@
	upx --best $@

$(BUILDDIR)/%.o: %.cpp
	$(CC) $(CFLAGS) $< -o $@

	if [ -d $(BUILDDIR) ]; then rm -Rf $(BUILDDIR); fi

distclean: clean
	rm -f *~
	rm -f $(EXECUTABLE)

This works fine as far as dynamically linking is involved.

Now I tried to find where I can look something up about static linking, and they all say "add the libraries to the end (after the -o) when linking. So I tried, but no avail. Also I read you need to use the flag '-static', but this even generates more errors about missing stuff (like missing 'delete' function and so on?!).

Is there someone who can help me with this problem?

Old 04-16-2006, 12:19 PM   #2
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 219

Rep: Reputation: 33
I am not quite sure, but what should work will be adding the library as a object
file to the linker. For this, give the path to the static library, when calling
the linker:

STATIC_OBJ= /usr/lib/libwx_gtk2u_adv-2.7.a


	upx --best $@
Old 04-16-2006, 12:37 PM   #3
Registered: Jul 2004
Location: Herent, Belgium, Europe, Earth
Distribution: Ubuntu 7.04
Posts: 102

Original Poster
Rep: Reputation: 15
I tried that too, but then the amount of errors went from 0 (links ok) -> 4800... [putting the static_obj before the objects] to 11700 [putting the static_obj after the objects].

But I am also seeing that if I put it there, it's not complaining about any wx-functions anymore... Only about some STL-functions? Which library should I include to counter that?


**EDIT** Adding all the default libraries where wxWidgets was compiled with (probably dynamically linked) to the LDFLAGS got me compiling the thing .

Thanks again!

Last edited by G00fy; 04-16-2006 at 12:50 PM.
Old 04-16-2006, 02:32 PM   #4
Registered: Sep 2003
Distribution: Debian
Posts: 49

Rep: Reputation: Disabled
CC=gcc -static

have a try.
Old 04-16-2006, 02:52 PM   #5
Registered: Jul 2004
Location: Herent, Belgium, Europe, Earth
Distribution: Ubuntu 7.04
Posts: 102

Original Poster
Rep: Reputation: 15

What I have now is:
LDFLAGS=-pthread -L/usr/lib -lstdc++ -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lfontconfig -lXinerama -lXi -lXrandr -lXext -lXcursor -lXfixes -lpango-1.0 -lXrender -lX11 -lgobject-2.0 -lgmodule-2.0 -ldl -lgthread-2.0 -lglib-2.0 -lXinerama -lpng -lz -ljpeg -ltiff

STATOBJS=/usr/lib/libwx_gtk2u_adv-2.7.a /usr/lib/libwx_gtk2u_core-2.7.a /usr/lib/libwx_baseu_net-2.7.a /usr/lib/libwx_baseu-2.7.a
This links ok, and dynamically links to all the LDFLAGS-libraries, and statically links to the wx-ones (exactly what I needed).

Now when I add your -static switch, I get 11792 errors .

Old 04-16-2006, 04:35 PM   #6
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

It sounds like you're squared away by specifying the full path name of your .a static libraries - cool.

But FYI, you can also alternate "-static" and "-dynamic" switches in the same link command, to specify you want certain libraries statically linked, and the rest dynamically linked.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
g++ statically link stdc++ problem berty Programming 1 05-16-2005 06:00 PM
statically link a network card driver into kernel stonux Linux - Hardware 0 04-07-2005 02:57 PM
fully statically linked posix thread program on PPC mmiglia Linux - Software 0 09-22-2004 07:06 AM
gcc link question eskimo22 Programming 2 03-03-2004 03:09 AM
Link errors from gcc CraigN Programming 2 02-27-2003 07:43 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:00 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration