使用__declspec(dllexport)从DLL导出函数 [英] Exporting functions from a DLL using __declspec(dllexport)

查看:1925
本文介绍了使用__declspec(dllexport)从DLL导出函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理项目(VS 2012解决方案),包括 joystic.cpp 。项目已创建为DLL,以便能够从另一个项目调用库。



我编辑了 resource.h

code>文件与宏:

  #ifdef LIBRARY_EXPORTS 
#define LIBRARY_API __declspec(dllexport)
#else
#define LIBRARY_API __declspec(dllimport)
#endif

为了能够通过排序导出函数:

  LIBRARY_API函数但是,我应该导出程序的所有函数,还是不导出?


b

解决方案


我应该导出程序的所有函数吗?


通常在设计库的接口时,应该只导出库的消费者所需的函数。



如果您导出每个函数,则需要记录每个函数。然而,库消费者可能不需要许多功能。许多函数都是私有的实现库。



此外,通过导出所有功能,您将使库的未来开发更加困难。如果您想将接口更改为您导出的其中一个私有函数,该怎么办?您必须引入另一个具有不同名称的函数,或者使用现有名称,从而打破二进制向后兼容性。这两个选项都不是特别有吸引力。



实质上,库的公共接口的表面积越大,维护任务越大。因此,您通常旨在提供尽可能小的公共接口,为库消费者提供所需的功能。


I 'm working on a project (VS 2012 solution) including joystic.cpp. Project has been created as DLL, in order to be able to call library from another project. The application receives immediate joypad data in exclusive mode via a dialog timer, displaying them in a dialog box.

I edited the resource.h file with macro:

#ifdef LIBRARY_EXPORTS
#    define LIBRARY_API __declspec(dllexport)
#else
#    define LIBRARY_API __declspec(dllimport)
#endif

in order to be able to export functions by ordering:

LIBRARY_API function();

However, should I export all functions of the program, or not?

解决方案

Should I export all functions of the program, or not?

Generally when designing the interface to a library, you should export just the functions that are needed by consumers of the library.

If you exported every function then you would need to document every function. However, many functions may not be needed by library consumers. Many functions will be private to the implementation of the library.

What's more, by exporting all functions you would be making future development of the library more difficult. What if you wanted to change the interface to one of these private functions that you exported? You would have to either introduce another function with a different name, or use the existing name and so break binary backwards compatibility. Neither of these options is particularly appealing.

In essence, the larger the surface area of your library's public interface, the larger your maintenance task. So you generally aim to provide the smallest possible public interface that provides the required functionality to library consumers.

这篇关于使用__declspec(dllexport)从DLL导出函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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