.def 文件导出 类的构造函数

u0116snail 2014-03-11 02:55:33
DLL 中写了一个C++类,但是并不是要导出整个类,只需要导出其中几个public函数就可以了。

在 DLL.cpp 中:
class CA
{
public:
__declspec(dllexport) CA(){}

public:
__declspec(dllexport) MyFun(){}
}


在 .def 文件中写了如下内容:
LIBRARY "MyDef"
EXPORTS
CCA
MyFun


为什么报错.def 文件错误,报错信息:unresolved external symbol CCA
.def文件中删除 CCA 就可以了,这是为什么?
...全文
384 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
class __declspec(dllexport) CA { .............. }; 加入以上关键字即可导出类
u0116snail 2014-03-12
  • 打赏
  • 举报
回复
引用 9 楼 huangxiangbo316 的回复:
貌似楼主只定义了CA函数,没有定义CCA函数
恩,这个例子是随手敲的,要改成 CCA
huangxiangbo316 2014-03-11
  • 打赏
  • 举报
回复
貌似楼主只定义了CA函数,没有定义CCA函数
赵4老师 2014-03-11
  • 打赏
  • 举报
回复
引用 6 楼 u011642451 的回复:
[quote=引用 3 楼 zhao4zhong1 的回复:] 英语也是一门计算机语言的说。 EXPORTS Home | Overview | FAQ | Details Syntax EXPORTS definitions This statement makes one or more definitions available as exports to other programs. EXPORTS marks the beginning of a list of export definitions. Each definition must be on a separate line. The EXPORTS keyword can be on the same line as the first definition or on a preceding line. The .DEF file can contain one or more EXPORTS statements. The syntax for an export definition is: entryname[=internalname] [@ordinal[NONAME]] [DATA] [PRIVATE] The optional keyword PRIVATE prevents entryname from being placed in the import library generated by LINK. It has no effect on the export in the image also generated by LINK. There are three methods for exporting a definition, listed in recommended order of use: The __declspec(dllexport) keyword in the source code An EXPORTS statement in a .DEF file An /EXPORT specification in a LINK command All three methods can be used in the same program. When LINK builds a program that contains exports, it also creates an import library, unless an .EXP file is used in the build.
感觉被你强奸了[/quote] 包养!
dll 导出函数名的那些事
关键字: VC++  DLL  导出函数 
经常使用VC6的Dependency查看DLL导出函数的名字,会发现有DLL导出函数的名字有时大不相同,导致不同的原因大多是和编译DLL时候指定DLL导出函数的界定符有关系。
VC++支持两种语言:即C/C++,这也是造成DLL导出函数差异的根源
我们用VS2008新建个DLL工程,工程名为"TestDLL"
把默认的源文件后缀 .CPP改为.C(C文件)
输入测试代码如下:
01 int _stdcall MyFunction(int iVariant)
02 {
03 return 0;
04 }
为了导出上面这个函数,我们有以下几个方法:
1. 使用传统的模块定义文件 (.def)
新建一个 后缀为.def的文本文件(这里建一个TestDll.Def),文件内容为:
LIBRARY TestDll
EXPORTS
MyFunction
在 Link 时指定输入依赖文件:/DEF:"TestDll.Def"
2. Visual C++ 提供的方便方法
在01行的int 前加入 __declspec(dllexport) 关键字
通过以上两种方法,我们就可以导出MyFunction函数。
我们用Dependency查看导出的函数:
第一种方法导出的函数为:
MyFunction
第二种方法导出的函数为:
_MyFunction@4
__stdcall会使导出函数名字前面加一个下划线,后面加一个@再加上参数的字节数,比如_MyFunction@4的参数(int iVariant)就是4个字节
__fastcall与 __stdcall类似,不过前面没有下划线,而是一个@,比如@MyFunction@4
__cdecl则是始函数名。
小结:如果要导出C文件中的函数,并且不让编译器改动函数名,用def文件导出函数。
下面我们来看一下C++文件
我们用VS2008新建个DLL工程,工程名为"TestDLL"
默认的源文件后缀为 .CPP (即C++文件)。
输入测试代码如下:
01 int _stdcall MyFunction(int iVariant)
02 {
03 return 0;
04 }
为了导出上面这个函数,我们有以下几个方法:
3. 使用传统的模块定义文件 (.def)
新建一个 后缀为.def的文本文件(这里建一个TestDll.Def),文件内容为:
LIBRARY TestDll
EXPORTS
MyFunction
在 Link 时指定输入依赖文件:/DEF:"TestDll.Def"
4. Visual C++ 提供的方便方法
在01行的int 前加入 __declspec(dllexport) 关键字
通过以上两种方法,我们就可以导出MyFunction函数。
我们用Dependency查看导出的函数:
第一种方法导出的函数为:
MyFunction
第二种方法导出的函数为:
?MyFunction@@YGHH@Z
可以看到 第二种方法得到的 导出函数名 并不是我们想要的,如果在exe中用显示方法(LoadLibrary、GetProcAddress)调用 MyFunction 肯定会失败。
但是用引入库(*.LIB)的方式调用,则编译器自动处理转换函数名,所以总是没有问题。
解决这个问题的方法是:
用VC 提供的预处理指示符 "#pragma" 来指定链接选项。
如下:
#pragma comment(linker, "/EXPORT:MyFunction=?MyFunction@@YGHH@Z")
这时,就会发现导出的函数名字表中已经有了我们想要的MyFunction。但我们发现原来的那个 ?MyFunction@@YGHH@Z 函数还在,这时就可以把 __declspec() 修饰去掉,只需要 pragma 指令即可。
而且还可以使如下形式:
#pragma comment(linker, "/EXPORT:MyFunction=_MyFunction@4,PRIVATE")
PRIVATE 的作用与其在 def 文件中的作用一样。更多的#pragram请查看MSDN。
小结:如果要导出C++文件中的函数,并且不让编译器改动函数名,用def文件导出函数。
同时可以用#pragma指令(C 中也可以用)。
总结:
C++编译器在生成DLL时,会对导出的函数进行名字改编,并且不同的编译器使用的改编规则不一样,因此改编后的名字也是不同的(一般涉及到C++ 中的重载等)。
如果利用不同编译器分别生成DLL和访问DLL的exe程序,后者在访问该DLL的导出函数时就会出现问题。如上例中函数MyFunction在C++编译器改编后的名字是?MyFunction@@YGHH@Z。我们希望编译后的名字不发生改变,这里有几种方法。
第一种方法是通过一个称为模块定义文件DEF来解决。
LIBRARY TestDll
EXPORTS
MyFunction
LIBRARY 用来指定动态链接库内部名称。该名称与生成的动态链接库名一定要匹配,这句代码不是必须的。
EXPORTS说明了DLL将要导出的函数,以及为这些导出函数指定的符号名。
第二种是定义导出函数时加上限定符:extern "C"
如:#define DLLEXPORT_API extern "C" _declspec(dllexport)
但extern "C"只解决了C和C++语方之间调用的问题(extern "C" 是告诉编译器,让它按C的方式编译),它只能用于导出全局函数这种情况 而不能导出一个类的成员函数。
同时如果导出函数的调用约定发生改变,即使使用extern "C",编译后的函数名还是会发生改变。例如上面我们加入_stdcall关键字说明调用约定(标准调用约定,也就是WINAPI调用约定)。
#define DLLEXPORT_API extern "C" _declspec(dllexport)
01 DLLEXPORT_API int _stdcall MyFunction(int iVariant)
02 {
03 return 0;
04 }
编译后函数名MyFunction改编成了_MyFunction@4
通过第一种方法模块定义文件的方式DLL编译后导出函数名不会发生改变。
DLL(动态库)导出函数名乱码含义
C++编译时函数名修饰约定规则:
  __stdcall调用约定:
  1、以"?"标识函数名的开始,后跟函数名;

  2、函数名后面以"@@YG"标识参数表的开始,后跟参数表;

  3、参数表以代号表示:
  X--void
  D--char
  E--unsigned char
  F--short
  H--int
  I--unsigned int
  J--long
  K--unsigned long
  M--float
  N--double
  _N--bool
  ....
  PA--表示指针,后面的代号表明指针类型,如果相同类型的指针连续出现,以"0"代替,一个"0"代表一次重复;
  4、参数表的第一项为该函数的返回值类型,其后依次为参数的数据类型,指针标识在其所指数据类型前;

  5、参数表后以"@Z"标识整个名字的结束,如果该函数无参数,则以"Z"标识结束。
  其格式为"?functionname@@YG*****@Z"或"?functionname@@YG*XZ",例如
                      int Test1(char *var1, unsigned long)-----"?Test1@@YGHPADK@Z"                      void Test2()-----"?Test2@@YGXXZ"

  __cdecl调用约定:
  规则同上面的_stdcall调用约定,只是参数表的开始标识由上面的"@@YG"变为"@@YA"。
  __fastcall调用约定:
  规则同上面的_stdcall调用约定,只是参数表的开始标识由上面的"@@YG"变为"@@YI"。
  如果要用DEF文件输出一个"C++"类,则把要输出的数据和成员的修饰名都写入.def模块定义文件
  所以...   通过def文件来导出C++类是很麻烦的,并且这个修饰名是不可避免的


u0116snail 2014-03-11
  • 打赏
  • 举报
回复
引用 4 楼 akirya 的回复:
应该写几个C的函数做封装 现在的做法方向都已经错了.
引用 4 楼 akirya 的回复:
应该写几个C的函数做封装 现在的做法方向都已经错了.
您的意思是: 应该导出整个类? 但是我不需要导出整个类,我只需要导出其中几个函数。 或者用C函数做壳子,导出一些C函数。但是不是那么容易用C函数做壳子的。
u0116snail 2014-03-11
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
英语也是一门计算机语言的说。 EXPORTS Home | Overview | FAQ | Details Syntax EXPORTS definitions This statement makes one or more definitions available as exports to other programs. EXPORTS marks the beginning of a list of export definitions. Each definition must be on a separate line. The EXPORTS keyword can be on the same line as the first definition or on a preceding line. The .DEF file can contain one or more EXPORTS statements. The syntax for an export definition is: entryname[=internalname] [@ordinal[NONAME]] [DATA] [PRIVATE] The optional keyword PRIVATE prevents entryname from being placed in the import library generated by LINK. It has no effect on the export in the image also generated by LINK. There are three methods for exporting a definition, listed in recommended order of use: The __declspec(dllexport) keyword in the source code An EXPORTS statement in a .DEF file An /EXPORT specification in a LINK command All three methods can be used in the same program. When LINK builds a program that contains exports, it also creates an import library, unless an .EXP file is used in the build.
感觉被你强奸了
u0116snail 2014-03-11
  • 打赏
  • 举报
回复
引用 4 楼 akirya 的回复:
应该写几个C的函数做封装 现在的做法方向都已经错了.
哦,多谢提醒。 请问我如何写C函数做壳子呢? 另外,应该如何把握方向,怎么做才是合适的?
  • 打赏
  • 举报
回复
应该写几个C的函数做封装 现在的做法方向都已经错了.
赵4老师 2014-03-11
  • 打赏
  • 举报
回复
英语也是一门计算机语言的说。 EXPORTS Home | Overview | FAQ | Details Syntax EXPORTS definitions This statement makes one or more definitions available as exports to other programs. EXPORTS marks the beginning of a list of export definitions. Each definition must be on a separate line. The EXPORTS keyword can be on the same line as the first definition or on a preceding line. The .DEF file can contain one or more EXPORTS statements. The syntax for an export definition is: entryname[=internalname] [@ordinal[NONAME]] [DATA] [PRIVATE] The optional keyword PRIVATE prevents entryname from being placed in the import library generated by LINK. It has no effect on the export in the image also generated by LINK. There are three methods for exporting a definition, listed in recommended order of use: The __declspec(dllexport) keyword in the source code An EXPORTS statement in a .DEF file An /EXPORT specification in a LINK command All three methods can be used in the same program. When LINK builds a program that contains exports, it also creates an import library, unless an .EXP file is used in the build.
u0116snail 2014-03-11
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_core_module.2d.definition_files.htm Module-Definition (.DEF) Files Home | Overview | FAQ | Details A module-definition (.DEF) file is a text file that contains statements for defining an .EXE file or DLL. Because LINK provides equivalent command-line options for most module-definition statements, a typical program for Win32 does not usually require a .DEF file. The descriptions of the module-definition statements give the command-line equivalent for each statement. See the following sections for more information: Rules for Module-Definition Statements NAME LIBRARY DESCRIPTION STACKSIZE SECTIONS EXPORTS VERSION
大哥,请不要欺负我不认识E文呀 你也别老拿着E文装牛B呀
赵4老师 2014-03-11
  • 打赏
  • 举报
回复
mk:@MSITStore:C:\MSDN98\98VS\2052\vccore.chm::/html/_core_module.2d.definition_files.htm Module-Definition (.DEF) Files Home | Overview | FAQ | Details A module-definition (.DEF) file is a text file that contains statements for defining an .EXE file or DLL. Because LINK provides equivalent command-line options for most module-definition statements, a typical program for Win32 does not usually require a .DEF file. The descriptions of the module-definition statements give the command-line equivalent for each statement. See the following sections for more information: Rules for Module-Definition Statements NAME LIBRARY DESCRIPTION STACKSIZE SECTIONS EXPORTS VERSION

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧