Visual Studio C++静态LIB功能暴露问题

发布于 2025-01-22 17:18:55 字数 1480 浏览 0 评论 0原文

我创建了使用静态库的示例。目标是此功能:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

胡大本事 2025-01-29 17:18:55

如果需要,则从.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.

风情万种。 2025-01-29 17:18:55

DLL具有明确的出口。 带有导出的文件

__declspec(dllexport)

或.DEF文件。在这种情况下,看来静态的lib不知道它是在DLL还是在EXE中,因此不使用extspec(dllexport)最佳解决方案是适用于.def文件为了使DLL明确导出该功能。

staticlibtest.h中的代码

__declspec(dllexport) void fnStaticLibTest();

与staticlibtest.cpp中的代码不一致,

void fnStaticLibTest(){

这不会导致函数导出。通常,包括一个声明函数 /结构以确保其一致的文件。

正确的表格是

void __declspec(dllexport) fnStaticLibTest(){

void __declspec(dllexport) fnStaticLibTest();

否则您将属性添加到void中。

A DLL has explicit exports. Either

__declspec(dllexport)

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

__declspec(dllexport) void fnStaticLibTest();

is inconsistent with the code in StaticLibTest.cpp

void fnStaticLibTest(){

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

void __declspec(dllexport) fnStaticLibTest(){

and

void __declspec(dllexport) fnStaticLibTest();

Otherwise you are adding the attribute to the void.

以酷 2025-01-29 17:18:55

这是我成功使用#pragma评论的片段。

DLL:

__declspec(dllexport)   void DllTestFunct();

...
  #pragma comment(linker, "/export:fnStaticLibTest")
  void DllTestFunct() {
    fnStaticLibrary();
    fnStaticLibTest();
    printf("Hello world (Dynamic library)!\n");
    }

DLL的依赖性:
标题文件夹:

XXX\StaticLibTest\StaticLibTest;
XXX\MainProgram\StaticLibrary;

DLL文件夹:

XXX\StaticLibTest\x64\Debug;
XXX\MainProgram\x64\Debug;

DLL输入:

StaticLibTest.lib;
StaticLibrary.lib

静态LIB:

  extern "C" void fnStaticLibTest();
...
extern "C" void fnStaticLibTest() {
    printf("Hello World! (Static library from another project)\n");
}

Here is the snippet I did use #pragma comment successfully.

DLL:

__declspec(dllexport)   void DllTestFunct();

...
  #pragma comment(linker, "/export:fnStaticLibTest")
  void DllTestFunct() {
    fnStaticLibrary();
    fnStaticLibTest();
    printf("Hello world (Dynamic library)!\n");
    }

DLL's dependency:
Header Folder:

XXX\StaticLibTest\StaticLibTest;
XXX\MainProgram\StaticLibrary;

DLL Folder:

XXX\StaticLibTest\x64\Debug;
XXX\MainProgram\x64\Debug;

DLL input:

StaticLibTest.lib;
StaticLibrary.lib

Static Lib:

  extern "C" void fnStaticLibTest();
...
extern "C" void fnStaticLibTest() {
    printf("Hello World! (Static library from another project)\n");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文