Visual C ++:在def文件中导出装饰函数名 [英] Visual C++: Exporting decorated function name in a def file

查看:167
本文介绍了Visual C ++:在def文件中导出装饰函数名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在def文件中导出装饰的函数名,如下所示:

  LIBRARY示例
EXPORTS
?? 0__non_rtti_object @ std @@ QAE @ ABV01 @@ Z = myfunc @ 1

问题是链接器在第一个@ -sign处删除函数名,并将
?? 0__non_rtti_object放在导出表中。我的问题是现在,如果有一个方法,包括
的@字符?我使用Visual Studio 2010.也许有人可以帮助我。



先感谢Hannes。

解决方案

前言



您没有回答我对使用.DEF文件的意见,所以我假设您不熟悉与 dllexport dllimport 限定符。与它们一起,没有必要使用.DEF文件导出符号。



如果特别需要.DEF文件,使使用 dllimport / dllexport 功能,请忽略以下内容。



使用 dllimport / dllexport



头像(例如 public.hpp ),写成:

  #ifdef MY_PROJECT_EXPORTS 
#define MY_PROJECT_API __declspec(dllexport)
#else
#define MY_PROJECT_API __declspec(dllimport)
#endif
pre>

这样,宏 MY_PROJECT_API 将启用导出/导入符号。例如,稍后在同一个 public.hpp 中,您可以声明:

  //全局变量
MY_PROJECT_API int myGlobalVariable;

//函数
MY_PROJECT_API void my_function();

//类或结构
class MY_PROJECT_API MyClass
{
public:
int i;
virtual int foo();
//等
};

然后,您需要做的是,在您的库的项目选项中,定义 MY_PROJECT_EXPORTS :这样,当你编译库时,上面的符号被声明为 dllexport ,当有人包括你的 public.hpp 头,以上符号将为 dllimport



如果你的代码是跨平台的( dllimport / dllexport 是MS编译器特性)围绕编译器测试。例如:

  #ifdef _MSC_VER 
//用于MS Visual Studio
#ifdef MY_PROJECT_EXPORTS
#define MY_PROJECT_API __declspec(dllexport)
#else
#define MY_PROJECT_API __declspec(dllimport)
#endif
#else
//其他编译器
#define MY_PROJECT_API
#endif



关于.DEF档案?



.DEF文件在之前使用,当可导出的C函数在Visual Studio上仍然是要走的路时。



对于强类型安全,C ++装饰其符号。



缺点是每个编译器都有自己的装饰方案



但是这样做的优点是:


  1. 您现在可以导出重载的/命名空间的函数/符号

  2. 参数类型是ABI的一部分, dllexport 功能具有以下优点:


    1. 在源代码级别,而不是使用另一个项目文件

    2. 程序员现在可以忽略特定的装饰方案(通常只关注链接器),同时从强类型安全C ++扩展到链接器。





    有关详细信息,请参阅:




    I want to export a decorated function name in a def file like this:

    LIBRARY Example
    EXPORTS
    ??0__non_rtti_object@std@@QAE@ABV01@@Z=myfunc @1
    

    The Problem is that the linker strips the function name at the first @-sign and places just "??0__non_rtti_object" into the export table. My question is now, if there is a way to include the @-characters as well? I use Visual Studio 2010. Maybe there is someone who can help me.

    Thanks in advance, Hannes.

    解决方案

    Preamble

    You didn't answer my comment about the use of the .DEF file, so I assume you must be unfamiliar with the the dllexport and dllimport qualifiers. With them, there is no need for the .DEF file to export symbols.

    If there is a particular need for the .DEF file that invalidate the use of the dllimport/dllexport feature, please ignore the following.

    How to use dllimport/dllexport?

    In your public header (say, public.hpp), write something like:

    #ifdef MY_PROJECT_EXPORTS
       #define MY_PROJECT_API __declspec(dllexport)
    #else
       #define MY_PROJECT_API __declspec(dllimport)
    #endif
    

    This way, the macro MY_PROJECT_API will enable the export/import of your symbols. For example, later, in the same public.hpp, you can declare:

    // A global variable
    MY_PROJECT_API int myGlobalVariable ;
    
    // A function
    MY_PROJECT_API void my_function() ;
    
    // A class or struct
    class MY_PROJECT_API MyClass
    {
       public :
          int i;
          virtual int foo() ;
          // etc.
    } ;
    

    Then, what you need to do is, in the project options of your library, define the MY_PROJECT_EXPORTS: This way, when you compile your library, the symbols above are declared dllexport, and when someone else includes your public.hpp header, the symbols above will be dllimport

    And if your code is cross-platform (dllimport/dllexport is a MS compiler feature), just wrap the defines above around a compiler test. For example:

    #ifdef _MSC_VER
       // For MS Visual Studio
       #ifdef MY_PROJECT_EXPORTS
          #define MY_PROJECT_API __declspec(dllexport)
       #else
          #define MY_PROJECT_API __declspec(dllimport)
       #endif
    #else
       // For other compilers
       #define MY_PROJECT_API
    #endif
    

    About the .DEF file?

    The .DEF file was used before, when exportable C functions were still "the way to go" on Visual Studio.

    For strong type safety, C++ decorate its symbols.

    The downside is that each compiler has its own decoration scheme (which never bothered me in 12-years of development), and that finding the exact, decorated name of a symbol can't be painful.

    But the merits of that is that:

    1. You can now export overloaded/namespaced functions/symbols
    2. the parameter types are part of the ABI, meaning the linker can verify you aren't screwing up or cheating with your types

    The dllimport and dllexport features have the following advantages:

    1. it enables the export to be done at source level, instead of using yet another project file
    2. the programmer can now ignore the particular decoration scheme (which usually only interests the linker), all the while profiting from the strong type safety of C++ extended to the linker.

    Sources

    For more information, see:

    这篇关于Visual C ++:在def文件中导出装饰函数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆