Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when statically linking libgmp on Linux

The GMP docs say that static linking may provide a small performance improvement.

I am having a problem getting it to staticly link libgmp on my Linux systems. I've narrowed down the issue I'm having to a tiny test case.

gmptest.c

#include <gmp.h>

int main(int argc, char** argv) {
    mpz_t foo;
    mpz_init(foo);
    return 0;
}

Makefile:

all: clean gmptest static

clean:
    rm -f *.s
    rm -f *.o
    rm -f gmptest
    rm -f static-gmptest

gmptest: Makefile gmptest.c
    gcc -std=c99 -O3 -lgmp gmptest.c -o gmptest

static: clean Makefile gmptest.c
    gcc -std=c99 -O3 -static /usr/lib/libgmp.a gmptest.c -o static-gmptest

The non-static binary is compiled and linked without any issues, but 'Make static' produces:

gcc -std=c99 -O3 -static /usr/lib/libgmp.a gmptest.c -o static-gmptest
/tmp/ccWSFke9.o: In function `main':
gmptest.c:(.text+0x8): undefined reference to `__gmpz_init'
collect2: ld returned 1 exit status
make: *** [static] Error 1

The library does exist:

chris@vostro:~/Dropbox/static$ ls -lA /usr/lib/libgmp.a 
-rw-r--r-- 1 root root 1041666 2010-02-26 13:20 /usr/lib/libgmp.a

I have also tried -lgmp for the static linking, but the error is the same.

This is all on Ubuntu 10.04 and 10.10 AMD64.

Can some enlighten me as to the obvious error I'm making?

Thanks,

Chris.

like image 348
fadedbee Avatar asked Jul 23 '26 14:07

fadedbee


1 Answers

Try

 gcc -std=c99 -O3 -static gmptest.c -lgmp -o static-gmptest

since libraries should always be linked in the good order, and after the program or object files using them.

like image 136
Basile Starynkevitch Avatar answered Jul 26 '26 07:07

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!