上篇的DLL实践没把为什么的问题解决, 现在给补上:
-------------------
1>. _cplusplus的问题
#ifdef _cplusplus
extern "C"{
#endif
#define GAN_EXPORT32 __declspec(dllexport)
GAN_EXPORT32 int add2(int a, int b);
#ifdef _cplusplus
}
#endif
-----
用这种方法就可以保持c/c++的兼容性, 当使用c++编译的使用编译器中会自动定义_cplusplus这个宏, 所以这个时候我们实际编译的文件就是:
extern "C"{
#define GAN_EXPORT32 __declspec(dllexport)
GAN_EXPORT32 int add2(int a, int b);
};
这个也就告诉了c++编译器在编译该段代码时要使用c的语法来进行编译一样。
对于cl.exe这个编译器我测试了一下,如果我们使用的文件名为.c他会自动按C的语法来编译。如果我们使用的是.cpp将自动按c++语法来编译, 这个时候我们也可以让他按照c的语法来编译的,那就得使用这个extern "C" { };了。
------------
一个很有趣的小事例:
gandll.c文件不变,把gandll.h文件中的extern "C"{ };这个内容写上和不写, 我这里的写就不要用什么_cplusplus这东西了:
A>. gandll.h为:
extern "C"
{
#define GAN_EXPORT32 __declspec(dllexport)
GAN_EXPORT32 int add2(int a, int b);
}
编译结果为:
C:\gandll>cl /LD /W4 gandll.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
gandll.c
gandll.h(7) : error C2059: syntax error : 'string'
C:\gan\ts\gandll>cl /LD /W4 gandll.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
gandll.c
gandll.h(1) : error C2059: syntax error : 'string'
没办法通过的:
B>. gandll.h为:
#define GAN_EXPORT32 __declspec(dllexport)
GAN_EXPORT32 int add2(int a, int b);
编译结果为:
C:\gandll>cl /LD /W4 gandll.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
gandll.c
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:gandll.dll
/dll
/implib:gandll.lib
gandll.obj
Creating library gandll.lib and object gandll.exp
用那个dumpbin可以看看哪个dll文件内容为:
C:\gandll>dumpbin /exports gandll.dll
Microsoft (R) COFF Binary File Dumper Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
Dump of file gandll.dll
File Type: DLL
Section contains the following exports for gandll.dll
0 characteristics
46F0BB0A time date stamp Wed Sep 19 14:00:42 2007
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00001000 add2
Summary
4000 .data
1000 .rdata
1000 .reloc
4000 .text
----------------------
再来个测试: 将哪个gandll.c文件内容不要变, 将文件名修改为gandll.cpp
A>. gandll.h为:
extern "C"
{
#define GAN_EXPORT32 __declspec(dllexport)
GAN_EXPORT32 int add2(int a, int b);
}
编译结果:
C:\gandll>cl /LD /W4 gandll.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
gandll.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:gandll.dll
/dll
/implib:gandll.lib
gandll.obj
Creating library gandll.lib and object gandll.exp
dumpbin查看的结果为:
C:\gandll>dumpbin /exports gandll.dll
Microsoft (R) COFF Binary File Dumper Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
Dump of file gandll.dll
File Type: DLL
Section contains the following exports for gandll.dll
0 characteristics
46F0BC0E time date stamp Wed Sep 19 14:05:02 2007
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00001000 add2
Summary
4000 .data
1000 .rdata
1000 .reloc
4000 .text
------------
B>. gandll.h为:
#define GAN_EXPORT32 __declspec(dllexport)
GAN_EXPORT32 int add2(int a, int b);
编译结果为:
C:\gandll>cl /LD /W4 gandll.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
gandll.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:gandll.dll
/dll
/implib:gandll.lib
gandll.obj
Creating library gandll.lib and object gandll.exp
dumpbin查看的结果为:
C:、gandll>dumpbin /exports gandll.dll
Microsoft (R) COFF Binary File Dumper Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
Dump of file gandll.dll
File Type: DLL
Section contains the following exports for gandll.dll
0 characteristics
46F0BC84 time date stamp Wed Sep 19 14:07:00 2007
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00001000 ?add2@@YAHHH@Z
Summary
4000 .data
1000 .rdata
1000 .reloc
4000 .text
你可以仔细比较以下有什么不一样的哦!
1>. 按照c++编译出来的哪个dll名字变了“?add2@@YAHHH@Z”, 哎, 我是使用不了c++的,没办法控制, 暂时就不去了解为什么了吧, 知道会改变输出函数名就可以了(既然没有经过我同意编译器自己来修改我的函数名, 不象话, 哈哈, 玩笑了)。
2>. 你如果使用.c为文件名那cl只会把他当成C文件来编译的。当你使用c, c++混合编程是一定要注意_cplusplus的问题哦。
阅读(3610) | 评论(1) | 转发(0) |