extern与__declspec(dllimport)的区别
来源:互联网 发布:c语言assert函数 编辑:程序博客网 时间:2024/05/24 01:46
- "extern" is for statically linked libraries?
- "__declspec(dllimport)" is for DLL (dynamically linked libraries)?
- both do actually the same job for their respective type of linking?
- you need to use both when you use import libraries (small .lib files that help linking with dll)?
1 Answer
extern
means that the entity has external linkage, i.e. is visible outside its translation unit (C or CPP file). The implication of this is that a corresponding symbol will be placed in the object file, and it will hence also be visible if this object file is made part of a static library. However, extern
does not by itself imply that the symbol will also be visible once the object file is made part of a DLL.
__declspec(dllexport)
means that the symbol should be exported from a DLL (if it is indeed made part of a DLL). It is used when compiling the code that goes into the DLL.
__declspec(dllimport)
means that the symbol will be imported from a DLL. It is used when compiling the code that uses the DLL.
Because the same header file is usually used both when compiling the DLL itself as well as the client code that will use the DLL, it is customary to define a macro that resolves to __declspec(dllexport)
when compiling the DLL and __declspec(dllimport)
when compiling its client, like so:
#if COMPILING_THE_DLL #define DLLEXTERN __declspec(dllexport)#else #define DLLEXTERN __declspec(dllimport)#endif
To answer your specific questions:
- Yes,
extern
alone is sufficient for static libraries. - Yes -- and the declaration also needs an
extern
(see explanation here). - Not quite -- see above.
- You don't strictly need the
extern
with a__declspec(dllimport)
(see explanation linked to above), but since you'll usually be using the same header file, you'll already have theextern
in there because it's needed when compiling the DLL.
- extern与__declspec(dllimport)的区别
- extern "c" __declspec(dllexport) __declspec(dllimport)
- extern "c" __declspec(dllexport) __declspec(dllimport)
- extern "C" __declspec(dllexport) __declspec(dllimport)
- extern "c" __declspec(dllimport) __declspec(dllexport)
- __declspec(dllexport) & __declspec(dllimport) 的区别和作用
- __declspec(dllimport)与__declspec(dllexport)
- __declspec(dllexport) 与 __declspec(dllimport)
- __declspec(dllexport)与__declspec(dllimport)
- __declspec(dllexport)与__declspec(dllimport)
- __declspec(dllexport)与__declspec(dllimport)
- __declspec(dllexport)与__declspec(dllimport)
- __declspec(dllexport) 与 __declspec(dllimport)
- _declspec(dllexport) 与__declspec(dllimport) 的使用说明
- 编写DLL函数时的 __declspec(dllexport) 与 __declspec(dllimport)
- __declspec(dllexport)和__declspec(dllimport)的不同与理解
- extern "C"和__declspec(dllexport)以及__declspec(dllimport) 和def的简单解析
- __declspec(dllimport) 的作用
- stack顺序C++
- JS中如何实现数字相加
- MySQL各存储引擎简介
- 自己的第一篇博客
- 【Matlab】自编代码实现感知机
- extern与__declspec(dllimport)的区别
- 范数、测度和距离.
- java 值传递 和 引用传递
- 【解方程】scipy.optimize.solve.
- Python 操作 Json 基础入门
- android repository中部分support库版本整理
- 洛谷 P1032 字串变换
- 最小化安装centos7要做的几件事情
- 拟阵:贪心原理(bzoj 3105: [cqoi2013]新Nim游戏)