导出DLL类和函数并将其导入Win32应用程序 [英] Export DLLs classes and functions and import them into Win32 Application

查看:141
本文介绍了导出DLL类和函数并将其导入Win32应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dll,其中包含一个在其中定义一些方法和变量的类。我将其标记为

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

__declspec(dllexport)

,然后在同一解决方案的win32应用程序项目中导入.h标头。我可以使用这些函数,但是当我尝试编译项目时,我遇到很多关于外部符号无法解决的错误。为什么?

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?

推荐答案

请在此处阅读有关使用宏完成此非常常见任务的标准方法: http://wiki.tcl.tk/8721

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

基本思想是您定义了一个宏,像这样说 MY_API

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);

在构建自己的声明函数主体的dll时,添加<$的定义c $ c> BUILD_MYAPI 生成。这样,当您包含其他一些dll的标头而不添加 BUILD_MYAPI dllexport

,因此减速度为 dllimport

使用Visual Studio编译时,可以在编译中添加宏定义,而无需更改源项目属性-> C / C ++-> Preprocesson->预处理程序定义

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

这篇关于导出DLL类和函数并将其导入Win32应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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