How to use the Microsoft Linker /Export parameter

吃可爱长大的小学妹 提交于 2019-12-24 13:52:36

问题


I would like to export functions from my object files manually with the Microsoft Linker. It works fine when I use the parameter for every function like this:

/Export:ExportedFunction1$qqsv /Export:ExportedFunction2$qqsv and so on...

The linker then automatically assigns the ords properly. However in the export table the actuall export name is "ExportedFunction1$qqsv/ExportedFunction2$qqsv/etc.." I tried the parameter doing like this:

/Export:ExportedFunction1$qqsv,1,ExportedFunction1 /Export:ExportedFunction2$qqsv,2,ExportedFunction2

But I think I'm using the parameters wrong?! How do I use the /Export parameter properly to assign my own names for the exports?

PS.: I'm using Microsoft (R) Incremental Linker Version 7.00.9210


回答1:


Here is a sample solution with a DEF file.

DLL-project:

CppLib.h:

#ifdef CPPLIB_EXPORTS
#define CPPLIB_API __declspec(dllexport)
#else
#define CPPLIB_API __declspec(dllimport)
#endif

CPPLIB_API double MyFunction1(double);

CppLib.cpp:

CPPLIB_API double MyFunction1(double dd)
{
    return dd;
}

CppLib.def:

LIBRARY

EXPORTS
MySuperFunction=MyFunction1 @1

Build DLL.

If we run dumpbin on CppLib.DLL, we'll get:

...
    ordinal hint RVA      name

          2    0 0001101E ?MyFunction1@@YANN@Z = @ILT+25(?MyFunction1@@YANN@Z)
          1    1 0001101E MySuperFunction = @ILT+25(?MyFunction1@@YANN@Z)
...

Console Application that uses CppLib.dll:

#include "CppLib.h"

#include <Windows.h>
#include <iostream>

int main()
{
    typedef double(*MY_SUPER_FUNC)(double);

    HMODULE hm = LoadLibraryW(L"CppLib.dll");
    MY_SUPER_FUNC fn1 = (MY_SUPER_FUNC)GetProcAddress(hm, "MySuperFunction"); // find by name
    MY_SUPER_FUNC fn2 = (MY_SUPER_FUNC)GetProcAddress(hm, MAKEINTRESOURCEA(1)); // find by ordinal

    std::cout << fn1(34.5) << std::endl; // prints 34.5
    std::cout << fn2(12.3) << std::endl; // prints 12.3

    return 0;
}



回答2:


#pragma comment(linker, "/EXPORT:ExportedFunction1$qqsv=_YouMangledFunction1@@")
#pragma comment(linker, "/EXPORT:ExportedFunction2$qqsv=_YouMangledFunction2@@")



回答3:


I do not believe you get that kind of contol with the /Export command-line switch, but you can do it with a .DEF file:

http://msdn.microsoft.com/en-US/library/hyx1zcd3(v=vs.80).aspx



来源:https://stackoverflow.com/questions/14713419/how-to-use-the-microsoft-linker-export-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!