在Windows上有关DLL导出/导入和外部的问题 [英] Question on DLL Exporting/Importing and Extern on Windows

查看:98
本文介绍了在Windows上有关DLL导出/导入和外部的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我对Windows dll有一些快速疑问。

Hey guys i have some quick questions on windows dll.

基本上,我正在使用ifdefs处理dllexport和dllimport,我的问题实际上是关于放置dllexports和dllimports以及extern关键字。

Basically i'm using the ifdefs to handle the dllexport and dllimport, my question is actually regarding the placement of the dllexports and dllimports as well as extern keyword.

我将dllimports / dllexports放在头文件中,但是我是否真的必须将dllexport和dllimports放在头文件中

I am putting the dllimports/dllexports on the header files but do I have to put the dllexport and dllimports on the actualy definition?

typedef怎么样?

What about for typedefs?

我是否将dllimport / dllexport放在前面?如

Do i put the dllimport/dllexport in front? as in

dllexport typedef map<string, int> st_map

关于extern关键字,我也看到它是这样使用的:

Also regarding the extern keyword I have seen it being used like this:

extern "C" {

dllexport void func1();

}

我也看到它是这样使用的:

I have also seen it being used like this:

extern dllexport func1();

一个包含 C,另一个不包含 C,我的问题是和有什么区别我需要使用它吗?如果可以,我是否同时将其用于dllexport和dllimport,是否还必须在头文件声明和定义中都使用它?

One includes the "C" and the other does not, my question is what is the difference and do i need to use it? If I do then do I use it for both dllexport and dllimport also do I have to use it on both the header file declarations and the definitions?

我的项目将作为共享库,它包含几个我想导出的类文件,一些我想导出的typdef和一些我也想将它们全部导出到dll的全局函数。

My project is going to be shared library, it contains several class files which I want to export, some typdefs i want to export and some global functions which I also want to export all into a dll.

请问有人启发我吗?

编辑:

好吧,我想我将发布一些摘要我已经做完了,还注意到我正在为linux和Windows都构建库,所以我要检查一下:

Okay i thought i'll post a small extract of what i've done, also notice that I am building the library for both linux and windows so I do a check for that:

mydll.h

#ifdef WINDOWS
#   ifdef PSTRUCT_EXPORT
#   define WINLIB __declspec(dllexport)
#   else
#   define WINLIB __declspec(dllimport)
#   endif
#else
#  define WINLIB
#endif

WINLIB void funct1();

现在在源代码中:

mydll.cpp

#define PSTRUCT_EXPORT

void funct1() <---- do i need to add WINLIB in front of it? 
                      Or is doing it in the header enough?


推荐答案

首先,您不需要导入或导出typedefs。只要它们都在双方都使用的头文件中,就可以了。您确实需要导入/导出函数和类定义。

First, you don't need to import or export typedefs. As long as they're in the header files that both sides use, you're good. You do need to import/export functions and class definitions.

大概在导入和导出代码时都使用相同的头文件,因此您可以使用一些makefile魔术来在每一侧定义一个预处理器宏,然后执行以下操作:

Presumably you use the same header files for both the importing and exporting code, so you could do some makefile magic to define a preprocessor macro on each side, then do something like this:

#if defined( LIBRARY_CODE )
#define MYAPI __declspec(dllexport)
#else
#define MYAPI __declspec(dllimport)
#endif

extern MYAPI void func1();
class MYAPI MyClass {
    ...
};

关于C与C ++函数,您可以这样做:

Regarding C vs. C++ functions, you can do this:

#if defined( __cplusplus__ ) // always defined by C++ compilers, never by C
#define _croutine "C"
#else
#define _croutine
#endif

extern _croutine void function_with_c_linkage();

请确保从C ++源文件(包含该函数的实现)导入此头文件,或者编译器不知道给它C链接。

Make sure you import this header file from your C++ source file (containing the implementation of this function) or the compiler won't know to give it C linkage.

这篇关于在Windows上有关DLL导出/导入和外部的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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