简体   繁体   English

由于未定义的引用,无法在 Windows 下编译共享库

[英]Can't compile shared library under Windows due to undefined reference

I'm using the same waf script, both for Linux and for Windows , to compile a shared-library for a program (which is able to load shared libraries at run-time).我为LinuxWindows使用相同的waf脚本来编译程序的shared-library (它能够在运行时加载共享库)。

To create this library I import some headers of the main program, so I can call main program's function in the library, and symbols are resolved at run-time, and this works under Linux.为了创建这个库,我导入了主程序的一些头文件,这样我就可以在库中调用主程序的函数,并且在运行时解析符号,这在Linux下有效。

Under Windows I'm not able to build the library because it says undefined reference for every function of the main program I call in the library.在 Windows 下,我无法构建库,因为它说我在库中调用的主程序的每个函数都undefined reference

What's the difference between Windows and Linux in that case?在这种情况下, WindowsLinux之间有什么区别? Why do I have to resolve symbols at linking-time under Windows ?为什么我必须在Windows下的链接时解析符号? Is there a flag or something like that to avoid this?有没有旗帜或类似的东西来避免这种情况?

Linux allows unresolved symbols in a shared library, but Windows does not. Linux 允许共享库中的未解析符号,但 Windows 不允许。 I have no good knowledge of the reasons of this difference.我不知道这种差异的原因。 In https://lists.gnu.org/archive/html/libtool/2007-04/msg00070.html for example you can see one opinion that on Linux it is "to allow circular references in shared libraries".例如,在https://lists.gnu.org/archive/html/libtool/2007-04/msg00070.html 中,您可以看到一种观点,即在 Linux 上“允许在共享库中进行循环引用”。

On Windows you can get the same behavior as on Linux with the /FORCE:UNRESOLVED linker parameter (see https://docs.microsoft.com/en-us/cpp/build/reference/force-force-file-output?view=msvc-170 ), but it has a restriction, namely your DLL must have an entry point.在 Windows 上,您可以使用/FORCE:UNRESOLVED链接器参数获得与在 Linux 上相同的行为(请参阅https://docs.microsoft.com/en-us/cpp/build/reference/force-force-file-output?view =msvc-170 ),但它有一个限制,即您的 DLL 必须有一个入口点。

On Linux you can get the same behavior as on Windows with -Wl,-no-undefined linker parameter.在 Linux 上,您可以使用-Wl,-no-undefined链接器参数获得与在 Windows 上相同的行为。 Additionally you can use a parameter -Wl,--no-allow-shlib-undefined to disallow undefined symbols only in shared libraries which you link against (see https://linux.die.net/man/1/ld ).此外,您可以使用参数-Wl,--no-allow-shlib-undefined仅在您链接的共享库中禁止未定义的符号(请参阅https://linux.die.net/man/1/ld )。

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

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