I'm not an expert on GCC internals, but maybe I can give you some clues
(which might not be totally accurate).
The function clock_gettime is defined in the mingw-w64 source code. You
can see the source code of that function it in the git repository git://
git.code.sf.net/p/mingw-w64/mingw-w64 inside
mingw-w64-libraries/winpthreads/src/clock.c. (Your example code has the
wrong prototype for clock_gettime, by the way.)
If you compile with the "-v" option, you can see that in the last step, GCC
invokes the collect2.exe with a lot of arguments specifying libraries:
-lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lpthread
-ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh
-lmoldname -lmingwex -lmsvcrt
I'm not sure where it gets this list of libraries, but it might be
hardcoded or it might have something to do with the specs file that you can
see by running "gcc -dumpspecs".
Then collect2.exe will pass these options on to the actual linker. The
linker will see that your program requires a symbol named clock_gettime,
and it will search through all those libraries in order to find the
symbol. I think it will find that symbol in the
library /mingw64/x86_64-w64-mingw32/lib/libpthread.dll.a.
If you run "objdump -t libpthread.dll.a | grep clock_gettime", you can see
that there are two symbols listed, clock_gettime and __imp_clock_gettime.
I'm not sure what is going on there, but somehow libpthread.dll.a contains
code for loading libwinpthread.dll at runtime and actually executing the
functions inside it, and that code gets included in your executable. If
you compile with the "-Wl,-Map=prog.map" option you can see more info about
__imp_clock_gettime and clock_gettime and where they got placed in your
program.
--David Grayson
On Sun, Apr 17, 2016 at 8:20 AM, Fabrice Popineau <
[email protected]> wrote:
> Hi list,
>
> From where does the dependency of programs built by gcc to
> libwinpthread-1.dll ?
> If I build a simple program like this :
>
> int main()
> {
> printf("foo");
> }
>
> and compile with :
>
> gcc -o foo.exe foo.c
>
> the resulting foo.exe program does not depend on libwinpthread-1.dll
> but if I add a reference to some function:
>
> char clock_gettime ();
>
> int main()
> {
> printf("foo");
> return clock_gettime();
> }
>
> then libwinpthread-1.dll gets linked in, without me changing the compile
> command.
> Where is this behavior configured?
>
> Thanks for any clue,
>
> Fabrice
>
>
> ------------------------------------------------------------------------------
> Find and fix application performance issues faster with Applications
> Manager
> Applications Manager provides deep performance insights into multiple
> tiers of
> your business applications. It resolves application problems quickly and
> reduces your MTTR. Get your free trial!
> https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
> _______________________________________________
> Msys2-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/msys2-users
>
>
------------------------------------------------------------------------------
Find and fix application performance issues faster with Applications Manager
Applications Manager provides deep performance insights into multiple tiers of
your business applications. It resolves application problems quickly and
reduces your MTTR. Get your free trial!
https://ad.doubleclick.net/ddm/clk/302982198;130105516;z
_______________________________________________
Msys2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/msys2-users