链接到链接到静态库和其他共享库的 DLL 的正确方法是什么?
问候, 我希望有人有耐心读完这篇文章。我手头有一个设置,这让我有点困惑。我有一个由 Eiffel 编译器生成的 C 源代码目录。 我想使用 Java 的输出,因此我需要一个用于 JNI 的 DLL,我将在其中实现一些 JNI 函数。当我编译 C 代码时,它给我一个静态库,它依赖于其他一些库。所以我的设置是:
myDLL.dll (C++) [取决于 --> staticLib.lib [取决于 --> (sharedlib1、sharedlib2...)
我的 C++ 项目是使用 Eclipse CDT 编译的,最终得到 1 个 dll。 我可以从 Java 中使用这个 DLL,没有任何问题。在执行过程中,它只需要路径中的另一个 DLL。静态库中的所有代码都编译到我的 dll 中,而对我的 DLL 的 Java JNI 调用又使用此代码。
我想使用另一个 C++ 项目中的相同 DLL。但是,当我仅链接到 DLL 时,链接器会抱怨找不到特定符号。该符号在静态库中声明为 extern,因此它必须位于静态库的依赖项之一中的某个位置。
我可以将 DLL 链接和编译到 C++ 项目的唯一方法也是链接到静态库,该静态库首先包含在 DLL 中。所以我最终得到一个 .exe,它包含两次静态库:一个在 DLL 中,另一个在 exe 中。
由于Java代码成功使用了DLL,我知道静态库被编译成DLL,但我无法避免在我的C++应用程序中包含它两次。我也对两次包含同一个库感到紧张。运行时如何处理事情?这会以意想不到的方式咬我吗?
我们将非常感谢您对管理此设置的建议。
最好的问候
瑟里夫
Greetings,
I hope someone has the patience to read this. I have a setup at hand, which is slightly confusing me. I have a C source code directory generated by an Eiffel Compiler.
I want to use this output from Java, so I need a DLL for JNI, in which I'll implement some JNI functions. When I compile the C code, it gives me a static library, which has dependencies to some other libraries. So my setup is:
myDLL.dll (C++) [depends on --> staticLib.lib [depends on --> (sharedlib1, sharedlib2...)
My C++ project is compiled with Eclipse CDT, and I end up with 1 dll.
I can use this DLL from Java without any problems It only needs one other dll in the path during execution. All the code from the static library is compiled into my dll, and Java JNI calls to my DLL in turn uses this code.
I wanted to use the same DLL from another C++ project. However, when I link only to DLL, the linker complains about not finding a particular symbol. This symbol is declared in the static library as extern, so it must be somewhere in one of the dependencies of the static library.
The only way I could link and compile the DLL to a C++ project is also by linking to static library, which was included in the DLL in the first place. So I end up with an .exe, that is containing the static library twice: one in the DLL, the other within the exe.
Since Java code successfully uses the DLL, I know that static library is compiled into DLL, but I can't avoid including it twice in my C++ app. I'm also nervous about including the same library twice. How are things handled during runtime? Can this bite me in an unexpected way?
Your recommendations to manage this setup would be much appreciated.
Best Regards
Seref
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的“第二个”dll 由于静态库中声明的符号而无法链接,则似乎第二个 dll 不应该看到该符号(为什么会这样),或者它还取决于静态库。
看来后者是不需要的,因此您应该尝试找出链接器通过哪个路径找到不需要的符号。可能是通过编译器在包含 DLL 的标头时看到的头文件...
If your 'second' dll cannot be linked because of a symbol declared in the static library, it seems that either the second dll shouldn't see that symbol (why does it), or it also depends on the static library.
It seems that the latter is unwanted, so you should try to find out through which path the linker finds the unwanted symbol. Probably through a header file the compiler sees when including yourDLL's headers...
如果您正在访问的为该静态库声明为 extern 的符号实际上并未被您为 DLL(消耗静态库)编写的代码使用,那么该符号肯定不在您的 DLL 中(因为您的 DLL 没有不需要它)。
If the symbol you are accessing that is declared as extern for that static library isn't actually used by the code you wrote for the DLL (which consumed the static library) then that symbol is most certainly NOT in your DLL (because you DLL didn't need it).