About dllexport and dllimport of c++ dynamic library

First recommend an auxiliary programming tool that is better than Microsoft Coplilot - Cursor, a new generation of auxiliary programming artifact based on GPT4 - Cursor, the official website said that it cooperates with OpenAI and is based on GPT4. I have experienced it myself, it is really good, it is worth trying.

Official website address:

Cursor | Build Fast

Regarding the use of the dllexport and dllimport keywords of the dynamic library. When to use dllexport and when to use dllimport.

dllexport and dllimport

is a keyword used to export and import functions and data in a dynamic link library (DLL). In the Windows operating system, a dynamic link library is a relocatable binary file that contains code and data that can be shared by multiple applications. The use of dynamic link libraries can reduce the size of executable files, but also improve code reusability and maintainability.

dllexport

Used to export functions or data into a DLL so that other applications can use them. For example, if you write a program called

MyFunction and mark it as dllexport, then other applications can use the LoadLibrary and GetProcAddress functions to load and call the function.

dllimport

Used to import functions or data from a DLL so that your application can use them. For example, if you want to use a function in another DLL in your application, you can mark the function as dllimport and use it in your code.

When using dllexport and dllimport, you need to pay attention to the following points:

  • dllexport and dllimport can only be used in Windows operating system.

  • dllexport and dllimport can only be used in dynamic link libraries (DLL).

  • dllexport and dllimport can only be used to export and import functions and data.

  • The syntax of dllexport and dllimport is the same, but the usage is different.

  • Both dllexport and dllimport require the name and visibility of a function or data to be specified.

  • Both dllexport and dllimport can specify the calling convention for functions or data.

  • Both dllexport and dllimport can specify the storage class of functions or data.

The lib file generated by dllexport can be either a static library or an import library. The generated .lib file contains information about exported functions and information used to resolve function addresses at link time. If you mark the exported function as dllexport, the compiler will generate a .lib file that contains information about the exported function. If you mark the imported function as dllimport, the compiler will generate a .lib file that contains information about the imported function.

The role of the .lib file is to resolve function addresses at link time. When you write an application and call a function in a dynamic link library, the compiler doesn't know where the address of the function is. Therefore, the compiler generates a symbol table containing the names of the functions and saves it in the .obj file. When you link your application, the linker looks up the function name in the symbol table and replaces it with the function's address. If you are using a statically linked library, the linker will simply copy the function's code into the application. If you're using an import library, the linker will load functions from the DLL at runtime.

To use a .lib file, you need to include it on the linker command line. For example, if you have a DLL called MyLibrary and you want to use functions from it in your application, you can use the following command line:

Among them, the /I option is used to specify the search path for header files, the /link option is used to specify the linker, and the /LIBPATH option is used to specify

The search path for .lib files, MyApplication.cpp is your application source file and MyLibrary.lib is your DLL's import library.

Q_DECL_IMPORT和Q_DECL_EXPORT

These are two macro definitions in Qt, which are used to realize the import and export of dynamic link libraries on different platforms. On the Windows platform, the import and export of the dynamic link library needs to use the __declspec(dllexport) and __declspec(dllimport) keywords, while on the Linux platform you need to use the __attribute__((visibility("default"))) keyword .

In order to facilitate cross-platform development, Qt defines Q_DECL_IMPORT and Q_DECL_EXPORT macros, which will be defined as different keywords on different platforms, thus realizing cross-platform dynamic link library import and export.

Q_DECL_IMPORT

It is used to declare imported functions or variables of a dynamic link library, which is usually used in header files. On the Windows platform, Q_DECL_IMPORT will be defined as __declspec(dllimport), on the Linux platform it will be defined as an empty macro. Using Q_DECL_IMPORT can make the functions and variables in the header file be imported correctly on different platforms.

Q_DECL_EXPORT

Used to declare an exported function or variable of a dynamic link library, which is usually used in source files. On the Windows platform, Q_DECL_EXPORT will be defined as __declspec(dllexport), on the Linux platform it will be defined as __attribute__((visibility("default"))).

Using Q_DECL_EXPORT can make the functions and variables in the source file be exported correctly on different platforms.

The dll dynamic library does not provide a lib file, how to use it?

If the dll library and the corresponding lib file are provided at the same time, the use of the dynamic library is very simple, and the compiler can load it globally for you when linking. If you only have a DLL file, but no LIB file, then you can use the LoadLibrary function to load the DLL file and get the function pointer. This method is called dynamic linking. Here is an example:

#include <windows.h>
#include <iostream>

typedef int (*MYPROC)(int);

int main() {
    HINSTANCE hinstLib;
    MYPROC ProcAdd;

    hinstLib = LoadLibrary(TEXT("MyDll.dll"));
    if (hinstLib != NULL) {
        ProcAdd = (MYPROC)GetProcAddress(hinstLib, "MyFunction");
        if (ProcAdd != NULL) {
            int result = ProcAdd(1);
            std::cout << "Result: " << result << std::endl;
        } else {
            std::cout << "Could not find function" << std::endl;
        }
        FreeLibrary(hinstLib);
    } else {
        std::cout << "Could not load library" << std::endl;
    }

    return 0;
}

In this example, we use the LoadLibrary function to load the MyDll.dll file. Then, we use the GetProcAddress function to get the address of the MyFunction function. Finally, we cast the function pointer to type MYPROC and call it to execute the function.

references:

Cursor | Build Fast

A better auxiliary programming tool than Microsoft Coplilot - Cursor - Programmer Sought

Guess you like

Origin blog.csdn.net/qq8864/article/details/129705375