简体   繁体   English

如何获取链接器以报告给定功能的链接位置

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

I have an issue trying to cross build win32 and win64 exes on a linux host. 我在尝试在Linux主机上交叉构建win32和win64 exe时遇到问题。

I am using the mingw cross build toolchains 我正在使用mingw cross build工具链

my .c file includes time.h in order to use clock_gettime() in main() 我的.c文件包含time.h以便在main()使用clock_gettime() main()

now this is a POSIX thing so no guarantee it is windows portable 现在这是POSIX所以不能保证它是Windows可移植的

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 我想做的是以某种方式使另一台计算机上的链接器告诉我它在哪里找到带有clock_gettime()的.dll。

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 为了让我查看新笔记本电脑上是否存在类似的.dll,以及其中是否有clock_gettime()符号可用

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. 我走了GIYF路线,但到目前为止还是空白。

Compile with -lrt which is needed for for glibc version < 2.17. 使用-lrt编译,这对于glibc版本<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. 另一台笔记本电脑上可能发生的情况是,它具有glibc> = 2.17的最新版本,其中clock_gettime()是libc的一部分。 But older in glibcs, it's a separate library. 但是在glibcs​​中,它是一个单独的库。 Hence, you needed to link it yourself. 因此,您需要自己链接它。

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 要在Linux主机上使用mingw工具链交叉构建Windows时使用<time.h>定义的clock_gettime() ,必须链接到pthread 而不是 rt

for example: 例如:

source code, example.c, looks like this: 源代码example.c看起来像这样:

#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: win32跨版本看起来像这样:

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 不幸的是,对于如何让链接器告诉我它在哪个库中找到了已成功链接到的函数,我不是明智的选择

ie 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. 即,如果我能以某种方式让链接器通知我它已在另一台成功链接的机器上的libpthread中找到clock_gettime(),那么我可以省去很多麻烦,以解决此机器上的链接错误问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM