LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Hi,


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):

Code:
EXECUTABLE=test
BUILDDIR=builddir

CC=gcc

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))




all: BUILDDIR_CREATION $(SOURCES) $(EXECUTABLE)

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

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

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

clean:
	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?


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

Rep: Reputation: 35
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:

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

[...]

$(EXECUTABLE): $(OBJECTS) 
	$(CC) $(LDFLAGS) $(OBJECTS) $(STATIC_OBJ) -o $@
	upx --best $@
 
Old 04-16-2006, 12:37 PM   #3
G00fy
Member
 
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?

Thanks.

**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
FinalFantasy
Member
 
Registered: Sep 2003
Distribution: Debian
Posts: 49

Rep: Reputation: Disabled
CC=gcc
->
CC=gcc -static

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

Original Poster
Rep: Reputation: 15
Nope

What I have now is:
Code:
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 .

Greetz
 
Old 04-16-2006, 04:35 PM   #6
paulsm4
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 PM.

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