Export DLLs classes and functions and import them into Win32 Application

百般思念 提交于 2019-12-24 09:26:19

问题


I have a dll with a class that define some methods and variables inside it. I marked it as

__declspec(dllexport)

and i imported the .h header inside a win32 application project in the same solution. I can use the functions but when I try to compile the project I have a lot of errors about external symbols not resolved. Why?


回答1:


Please read about the standard way of using macros for this very common task here: http://wiki.tcl.tk/8721

The basic idea is that you define a macro, say MY_API like so:

    #ifdef BUILD_MYAPI
    #    define MY_API __declspec(dllexport)
    #else
    #    define MY_API __declspec(dllimport)
    #endif

When you declare a function or a class in the header file you do this:

void MY_API myApiFunction(int x);

When you build your own dll which declares the body of the function, you add the definition of BUILD_MYAPI to the build. This makes all declerations to be dllexport
when you include the header from some other dll you don't add BUILD_MYAPI so the decelerations are dllimport
When compiling with visual studio, you can add a macro definition to the compilation without changing the source from project properties -> C/C++ -> Preprocesson -> Preprocessor definitions




回答2:


For the application where you want to import that class, you will need to mark the class as

__declspec(dllimport)

Instead of dllexport.

You must also make sure to link with the import library of the DLL (a .lib file).



来源:https://stackoverflow.com/questions/4983835/export-dlls-classes-and-functions-and-import-them-into-win32-application

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