Click to See Complete Forum and Search --> : gcc: static compiling


threadhead
03-04-2006, 05:06 PM
hello!

i have multiple source files like 1.c, 2.c and 3.c where 1.c calls functions that lie in 2.c and 3.c.
some of the source files access the openssl library with function calls.

when i am compiling the package to a dynamically linked elf this is no problem:

gcc -c -o 1.o 1.c
gcc -c -o 2.o 2.c
gcc -c -o 3.o 3.c
gcc -lssl -o a.out 1.o 2.o 3.o

When i try the last step with static:
gcc -static -lssl -o a.out 1.o 2.o 3.o
i get lots of complaints about undefined references like:
: undefined reference to `RAND_status'
1.o(.text+0x13b): In function `prepare_openssl':
: undefined reference to `RAND_load_file'
1.o(.text+0x15d): In function `create_pem':
: undefined reference to `DH_new'
1.o(.text+0x195): In function `create_pem':
: undefined reference to `DH_generate_parameters'
...
...


so my question is, how i can compile this to a statically linked elf binary?

thanks in advance

evac-q8r
03-05-2006, 08:10 AM
Hmm... My first thought is whether or not the ssl library that you are linking is available only as a shared object *.so file. If so, I can imagine that you will get some problems. When you use the -static option, my guess is the linker searches for the same library but with the *.a (a static library) extension. Not all libraries come in both formats.

Peace... EVAC

bwkaz
03-05-2006, 03:00 PM
Do you have a libssl.a file anywhere? If not, it's probably part of openssl-dev (or some similarly-named package).

What happens if you put -lssl after 1.o, 2.o, and 3.o? So you would do:

gcc -static -o a.out 1.0 2.0 3.0 -lssl

Sometimes gcc (actually the linker, but whatever) is picky about what order you use when you give it the object files.