How to get linker to report where it is linking to for a given function

220 views Asked by At

I have an issue trying to cross build win32 and win64 exes on a linux host.

I am using the mingw cross build toolchains

my .c file includes time.h in order to use clock_gettime() in main()

now this is a POSIX thing so no guarantee it is windows portable

however, on another laptop with a similar (but obviously not identical) setup it does compile and link no problem

on this laptop (a new one I am migrating to) I get a linker error:

undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status

what I would like to be able to do is somehow have the linker on the other machine tell me where it is finding the .dll with clock_gettime() in it

In order for me to see whether the similar .dll is present on the new laptop and whether the clock_gettime() symbol is avaiable in it

Is it possible to get the linker to report this info, some sort of verbose mode perhaps. I've gone down the GIYF route but drawn a blank thus far.

2

There are 2 answers

0
bph On BEST ANSWER

To use clock_gettime() as defined in <time.h> when cross building for windows using mingw toolchain on a linux host you must link to pthread not rt

for example:

source code, example.c, looks like this:

#include <time.h>
...
struct timespec t1;
...
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);

native build looks like this:

gcc example.o -lrt -o example

win32 cross-build looks like this:

i686-w64-mingw32-gcc -I/usr/local/i686-w64-mingw32/include  example.o -L/usr/local/i686-w64-mingw32/bin -lpthread -lws2_32 -o example.exe

Unfortunately, I am none the wiser on how to get the linker to tell me in which library it has found a function that it has successfully linked to

i.e. if I could have somehow got the linker to inform me that it had found clock_gettime() in libpthread on my other machine that was successfully linking, I could have saved a lot of messing about fixing the link error issue on this machine.

6
P.P On

Compile with -lrt which is needed for for glibc version < 2.17.

What probably happens on the other laptop is that it has a recent version of glibc >=2.17 in which the the clock_gettime() is part of the libc. But older in glibcs, it's a separate library. Hence, you needed to link it yourself.