11.4/gcc4.5.1/glib2.14: -static option creates undefined reference to clock_gettime()

Following command compiles/links fine and program runs without errors. When I add -static option, I get link error: undefined reference clock_gettime(). No other errors or differences exist and everything else links OK.

gcc -Wl,-Map,Ragent.map -O0 -fms-extensions -I . -pthread -L/usr/lib64 -l rt -l pthread -g -Wa,-a,-ad Ragent.c -o Ragent > Ragent.cod

NOTE: both rt and pthread libs have a clock_gettime() in them, but linker will only pull function from rt lib(without -static specified)

  1. Why wont clock_gettime() link with -static(even though everything else links fine)?
  2. Is there a way to dump exactly what is in a lib, i.e. what is the exact prototype of the functions included and what criteria the linker uses?
  3. Do I need special static libs? Download from where?

thanks,

Because you don’t have rt.a installed (see #3 below).

  1. Is there a way to dump exactly what is in a lib, i.e. what is the exact prototype of the functions included and what criteria the linker uses?

You can use objdump and other binutils, though this is not often needed - in this case, the errors are simply because the linker can’t find the static lib, so the reference is unresolvable. But I do know what you mean - sometimes you can’t help but wonder “is it really in there?” If I really can’t believe the linker won’t resolve correctly, I usually check with:

objdump -d /usr/lib64/librt.a | grep clock_gettime
strings /usr/lib64/librt.a | grep clock_gettime
  1. Do I need special static libs? Download from where?

Yep, you need glibc-devel-static, which you can get from factory, see: software.opensuse.org: Search Results

thanks,

Your welcome! Happy Linking.

Cheers,
Pete

Thanks for the quick response.

> Because you don’t have rt.a installed (see #3 below).

/usr/lib64/librt.a already existed, I couldn’t link without -static otherwise.

I did install glibc-devel-static-2.14(which needed glibc-devel-2.14(which needed glibc-2.14(which already existed))) and still have same symptom.

Hi there,

Ahh . . I see - sorry. I must admit, I’m not much of a pthreads guy, so I’m afraid I don’t have a lot of other suggestions. Though it looks correct to me, have your tried altering the order libraries you are linking to (-l pthread -l rt)? Just thinking of the link order of libraries, as for example in An Introduction to GCC - Link order of libraries

Sorry I can’t be of more help than that.

Cheers,
Pete

Cheers,
Pete