Visual Studio C++静态LIB功能暴露问题
我创建了使用静态库的示例。目标是此功能:
- dlltestfunctdll.dll链接static_lib.lib(包含fnstaticlibrary())
- example.exe dlltestfunctdll.dll.dll(包含dlltestfunctfunct()
- main()main()aim())直接调用fnstaticlibrary()和dllTestFunctfunctfunct()和dllTestFunctfunct()。
- dlltestfunct()直接调用fnstaticlibrary()。
我不明白一件事,这仅在fnstaticlibrary()与main()和dlltestfunct()的项目中。
如果我创建了另一个解决方案,
- dlltestfunctdll.dll链接也static_lib.lib(包含fnstaticlibtest())
- main()新new new new dyally也直接调用fnstaticlibtest()。
- dlltestfunct()新呼叫直接调用fnstaticlibtest()。
dlltestfunct()能够调用fnstaticlibtest(),而main()无法 将fnstaticlibtest()称为链接器。但是我可以在dlltestfunct()内调用fnstaticlibtest()。
我使用DumpBin查看导出了哪些功能:
dumpbin /EXPORTS "C:\\path\\DllTestFunctDll.dll"
输出:
ordinal hint RVA name
1 0 00001000 ?dllTestFunct@@YAXXZ = ?dllTestFunct@@YAXXZ (void __cdecl DllTestFunct(void))
2 1 00001070 ?fnStaticLibrary@@YAXXZ = ?fnStaticLibrary@@YAXXZ (void __cdecl fnStaticLibrary(void))
您可以看到输出内部缺少fnstaticlibtest()。
在两个静态lib项目中,我都会通过“ __declspec(dllexport)导出功能”。
我认为问题在视觉工作室内。你知道如何解决吗?
预先感谢您的回答。
CODE (VS17 solution) LINK: https://github.com/Ales5475/StaticLibProblemExample< /a>
I have created example of using static libraries. The goal is this functionality:
- DllTestFunctDll.dll links static_lib.lib (contains fnStaticLibrary())
- example.exe links DllTestFunctDll.dll (contains dllTestFunct())
- main() calls directly fnStaticLibrary() and dllTestFunct().
- dllTestFunct() calls directly fnStaticLibrary().
I don't understand one thing, this works only if fnStaticLibrary() is inside same project as main() and dllTestFunct().
If I create another solution that
- DllTestFunctDll.dll links also static_lib.lib (contains fnStaticLibTest())
- main() newly also calls directly fnStaticLibTest().
- dllTestFunct() newly also calls directly fnStaticLibTest().
dllTestFunct() is able to call fnStaticLibTest() and main() is unable to call fnStaticLibTest() due to linker. But I am able to call fnStaticLibTest() inside dllTestFunct().
I used dumpbin to see what functions are exported:
dumpbin /EXPORTS "C:\\path\\DllTestFunctDll.dll"
Output:
ordinal hint RVA name
1 0 00001000 ?dllTestFunct@@YAXXZ = ?dllTestFunct@@YAXXZ (void __cdecl DllTestFunct(void))
2 1 00001070 ?fnStaticLibrary@@YAXXZ = ?fnStaticLibrary@@YAXXZ (void __cdecl fnStaticLibrary(void))
You can see that fnStaticLibTest() is missing inside the output.
In both static lib projects I export function by "__declspec(dllexport) ".
I assume that the problem is inside the Visual Studio. Do you know how to solve it?
Thank you in advance for your answers.
CODE (VS17 solution) LINK: https://github.com/Ales5475/StaticLibProblemExample
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果需要,则从.lib添加对象模块。如果DLL不调用(或引用)编译单元(C/CPP文件)中的任何项目,则不会将其添加到DLL中,
此行为对库很重要,因为它可以确保在链接到C/C ++时运行时间,那么您不会导入整个库。只是需要的。
在您的情况下,您有一个不需要测试功能的DLL,并且一个.exe希望DLL具有它。构建DLL时,测试功能没有可见的要求。
An object module is added from a .lib, if it is required. If the dll does not call (or reference) any item in a compilation unit (c/cpp file), it won't be added to the dll
This behavior is important for libraries, as it ensures that when linking against the C/C++ runtimes, then you don't import the whole of the libraries. Just those which are required.
In your case you have a DLL which doesn't require the test function, and a .EXE which expects the DLL to have it. There is no visible requirement for the test function when the DLL is being built.
DLL具有明确的出口。 带有导出的文件
或.DEF文件。在这种情况下,看来静态的lib不知道它是在DLL还是在EXE中,因此不使用
extspec(dllexport)
最佳解决方案是适用于.def文件为了使DLL明确导出该功能。staticlibtest.h中的代码
与staticlibtest.cpp中的代码不一致,
这不会导致函数导出。通常,包括一个声明函数 /结构以确保其一致的文件。
正确的表格是
,
否则您将属性添加到
void
中。A DLL has explicit exports. Either
or a .DEF file with exports. In this case, it appears that the static lib doesn't know if it is in a DLL or the EXE, so isn't decorated with
declspec(dllexport)
the best solution is for the .def file for the DLL to explicitly export the function.The code in StaticLibTest.h
is inconsistent with the code in StaticLibTest.cpp
This will not cause the function to be exported. In general include a file which declares a function / structure to ensure it is consistent.
The correct form is
and
Otherwise you are adding the attribute to the
void
.这是我成功使用
#pragma评论
的片段。DLL:
DLL的依赖性:
标题文件夹:
DLL文件夹:
DLL输入:
静态LIB:
Here is the snippet I did use
#pragma comment
successfully.DLL:
DLL's dependency:
Header Folder:
DLL Folder:
DLL input:
Static Lib: