Making a statically linked executable

hi, i am trying to build something with all libraries statically linked. i had to rebuild mesa to enable static linking because i was getting an error like:
/usr/bin/ld: could not find -lglut
After i rebuilt mesa i no longer got this error, but now at link time i am getting this error:
In function ‘InterruptibleXNextEvent’
undefined reference to ‘XFlush’
There are several more undefined references also. Does anyone know how to do static linking correctly, or what this problem is exacty??

Sounds like you aren’t linking in the X libraries (-lX11).

To build a statically linked executable, use -static as a link arg.

thanks rts, i appreciate your frequent support to this forum, but i have been using all the X libraries, i was linking correctly before and experienced the trouble when i added the -static flag here is what my linking looks like:
g++ -static -o myexe file1.o file2.o file3.o file4.o -L/usr/lib/X11R6/lib -lX11 -lXi
-lXmu -lm -lGL -lGLU -lglut
One thing that i know is a potential problem are libraries that may be mutually dependent. i know that this can be resolved by listing a library more than once but i am not certain of what mutual dependendencies exist, if any. if anyone has successfully statically linked anything using the glut library could you please share how you linked. tia

I’m afraid I don’t have any static libs for GL development installed on my system, but I would try moving -lglut around on the command line.

Another option, although it will make your link a lot slower, is to put:




around the list of libs you link with:

gcc … -Wl,–whole-archive -lX11 … -lglut -Wl,–no-whole-archive

thanks for the help, i ended up rebuilding mesa --without-glide during the configure because i did not have a static version of libglide3. when i did link i ended up having to repeat libs several times like:
g++ -static -o myexe file1.o file2.o -L/usr/X11R6/lib -lm -lX11 -lXi -lXmu -lXext -lXt -lSM -lICE -lGL -lGLU -lglut -lpthread -lm -lX11 -lXi -lXmu -lXext -lXt -lSM -lICE -lGL -lGLU -lglut -lpthread …(repeat sequence once more)
there may be a shorter sequence but i did have to repeat it 3 times to account for all dependencies. just thought i would give my solution for all those people that search archives before posting something repeatedly

The -Wl,–whole-archive option avoids having to repeat libraries like that. So take your sequence of libs, put


before and


after and then it won’t matter what order you put them in between those two options.

oooooh, i see what thats for now, thanks again

See the example below, taken directly from
my own makefiles. To compile statically or dynamically, you just have to change the commented line.

When you compile dynamically a program, the order of the libraries is not important. However, when you compile statically, each library must be referenced after the libraries that it deppends on (the static linker starts reading from the END of the line).

LIBS = -lexpat -lgtkgl -lgtk -lgdk -lgmodule -lglib -ldl -lGLU
-lGL -lXext -lX11 -lXi -lXmu -lvga -lm

$(EXE): $(GAMGI_o)

$(CC) -o $@ $^ $(PATH_L) -Wl,-Bstatic $(LIBS)

    $(CC) -o $@ $^ $(PATH_L) -Wl,-Bdynamic $(LIBS)

%.o : %.c $(GAMGI_h)
$(CC) -c $(PATH_H) $< -o $@

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.