zoukankan      html  css  js  c++  java
  • .def文件与__declspec(dllexport)

    A module-definition (.def) file is a text file containing one or more module statements that describe various attributes of a DLL. If you are not using the __declspec(dllexport) keyword to export the DLL's functions, the DLL requires a .def file.
    A minimal .def file must contain the following module-definition statements:
    The first statement in the file must be the LIBRARY statement. This statement identifies the .def file as belonging to a DLL. The LIBRARY statement is followed by the name of the DLL. The linker places this name in the DLL's import library.

    The EXPORTS statement lists the names and, optionally, the ordinal values of the functions exported by the DLL. You assign the function an ordinal value by following the function's name with an at sign (@) and a number. When you specify ordinal values, they must be in the range 1 through N, where N is the number of functions exported by the DLL. If you want to export functions by ordinal, see Exporting Functions from a DLL by Ordinal Rather Than by Name as well as this topic.
    For example, a DLL that contains the code to implement a binary search tree might look like the following:

    LIBRARY   BTREE
    EXPORTS
       Insert   @
    1
       Delete   @
    2
       Member   @
    3
       Min   @
    4

    Microsoft introduced __export in the 16-bit compiler version of Visual C++ to allow the compiler to generate the export names automatically and place them in a .lib file. This .lib file can then be used just like a static .lib to link with a DLL.

    In newer compiler versions, you can export data, functions, classes, or class member functions from a DLL using the __declspec(dllexport) keyword. __declspec(dllexport) adds the export directive to the object file so you do not need to use a .def file.
    This convenience is most apparent when trying to export decorated C++ function names. Because there is no standard specification for name decoration, the name of an exported function might change between compiler versions. If you use __declspec(dllexport), recompiling the DLL and dependent .exe files is necessary only to account for any naming convention changes.
    Many export directives, such as ordinals, NONAME, and PRIVATE, can be made only in a .def file, and there is no way to specify these attributes without a .def file. However, using __declspec(dllexport) in addition to using a .def file does not cause build errors.
    To export functions, the __declspec(dllexport) keyword must appear to the left of the calling-convention keyword, if a keyword is specified. For example:

    __declspec(dllexport) void __cdecl Function1(void);

    To export all of the public data members and member functions in a class, the keyword must appear to the left of the class name as follows:

    class __declspec(dllexport) CExampleExport : public CObject
    {  
    class definition };



    Reference:
    1. http://msdn.microsoft.com/en-us/library/d91k01sh(VS.80).aspx 
    2. http://msdn.microsoft.com/en-us/library/a90k134d(VS.80).aspx

  • 相关阅读:
    痞子衡嵌入式:恩智浦i.MX RTxxx系列MCU特性那些事(1)- 概览
    痞子衡嵌入式:16MB以上NOR Flash使用不当可能会造成软复位后i.MXRT无法正常启动
    《痞子衡嵌入式半月刊》 第 12 期
    不能错过的分布式ID生成器(Leaf ),好用的一批!
    实用!一键生成数据库文档,堪称数据库界的Swagger
    安排上了!PC人脸识别登录,出乎意料的简单
    又被逼着优化代码,这次我干掉了出入参 Log日志
    图文并茂,带你认识 JVM 运行时数据区
    一文说通C#中的异步编程补遗
    一文说通C#中的异步编程
  • 原文地址:https://www.cnblogs.com/whyandinside/p/1560948.html
Copyright © 2011-2022 走看看