打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
利用C语言创建和使用DLL文件
 有感于讲C语言的DLL文件的文章很少,自己查了半天,写了这么个非常简单的教程。自己也是摸C语言不久,依然感觉处于编程苦手的阶段。
 
1)为什么使用DLL文件
 
C语言复用代码有很多的形式,利用动态链接库(DLL)来复用代码也是一种很有效的做法。使用DLL相比利用静态库来复用代码有几点不同:
 
a. 可以不用写 header File,但是在编译过程中需要在编译器里把文件链接起来;
 
b. 更加灵活,可以只改动和编译DLL文件的内容,而不用对程序其他部分进行修改;
 
c. 利用DLL文件可以方便地与其他语言进行链接(比如Python)。
 
2)创建DLL及C语言调用程序
 
目前看来,创建 DLL 文件和创建普通c语言文件没有什么不同。创建 C++ 的 DLL 文件要更复杂一些,C则相对简单。照着 C 代码的格式写一个文件(注:C++ 似乎会不同,微软就有教程,可以查阅VS的帮助文档)。
 
以下是一个实例,文件名为 TestDll.c
 
//TestDll.c 

#include <</font>stdio.h>

int hello()
{
    printf("Hello from DLL\n");
}

int SumNumbers(int a, int b)
{
    intc;
    c=a+b;
    returnc;
}
 
然后写一个主程序来调用,文件名为 UseTestDll.c
 
//UseTestDLL.c 

#include <</font>stdio.h>

int main()
{
    hello();
    hello();
    inta=2,b=3;
    intc;
    c=SumNumbers(a,b);
    printf("c= %d.\n",c);
}
 
搞定。
 
3)编译及运行
 
测试使用的是 MinGW 下的 gcc 编译器。
 
a. 编译 DLL 文件
 
先将 c 文件编译成 o 文件,然后再讲 o 文件编译成为 DLL 文件,在 cmd 里面代码如下:
 
gcc -c TestDLL.c
gcc -shared -o TestDll.dll TestDll.o
这样就得到了 TestDll.dll 文件,如果文件多的话可以写个Batch文件来搞定。
 
b. 编译使用文件
 
gcc -o UseTestDllUseTestDll.c -L./ -lTestDll
 
这样就得到了 UseTestDll.exe 文件。UseTestDll.exe 和 TestDll.dll形成了程序的两个部分,缺一不可。
 
运行一下:
 


 
4)在Python中使用已有的Dll文件
 
DLL文件一样可以在Python中使用。我们可以利用python自带的ctypes模块(python2.5后自带,之前得自己再去下,不过现在也没有人用2.5之前的了吧)。下面是一个示例文件,文件名为UseCDll.py
 
from ctypesimport *

# Simple Test on c_int object
i=c_int(5)
print i
print i.value
i.value=10
print i.value

# Import Dll
TestDll=CDLL('TestDll.dll')

# Test Print Function
TestDll.hello()

# Test variable dilivery
a=c_int(4)
b=c_int(6)
c=TestDll.SumNumbers(a,b)
print c
函数说明:
  • c_int() 在python下创建c的int类型对象,因为python的数据类型和c的数据类型需要转换
  • CDLL() 读入DLL文件,并将其转化为一个对象,利用 对象.函数 的形式调用DLL里面的函数
运行一下:


成功实现了在Python下使用DLL文件,这种方法可以减少代码重复开发,同时由于C的速度比Python大很多,还可以用这个方法对Python进行加速。
 
5)APPENDIX
 
关于gcc编译命令的一些说明,来自gcc官方文档。
 

-shared
 
Produce a shared object which can then be linked with otherobjects to form an executable. Not all systems support this option.For predictable results, you must also specify the same set ofoptions used for compilation (‘-fpic’, '-fPIC’, or modelsuboptions) when you specify this linker option.1
 
1 On some systems, ‘gcc -shared’ needs to build supplementarystub code for constructors to work. On
multi-libbed systems, ‘gcc -shared’ must select the correctsupport libraries to link against. Failing to
supply the correct flags may lead to subtle defects. Supplyingthem in cases where they are not necessary
is innocuous
 

-Ldir
 
Add directory dir to the list of directories to besearched for ‘-l’.
 

-o file
 
Write output to file. This is the same as specifying file asthe second non-option argument to cpp. gcc has a differentinterpretation of a second non-option argument, so you must use‘-o’ to specify the output file.
 

-c
 
Compile or assemble the source files, but do not link. Thelinking stage simplyis not done. The ultimate output is in the formof an object file for each source file. By default, the object filename for a source file is made by replacing the suffix ‘.c’, ‘.i’,‘.s’, etc., with ‘.o’.
Unrecognized input files, not requiring compilation orassembly, are ignored.
 

-llibrary
-l library
 
Search the library named library when linking. (The secondalternative with the library as a separate argument is only forPOSIX compliance and is not recommended.) It makes a differencewhere in the command you write this option; the linker searches andprocesses libraries and object files in the order they arespecified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file‘foo.o’ but before
‘bar.o’. If ‘bar.o’ refers to functions in ‘z’, thosefunctions may not be loaded. The linker searches a standard list ofdirectories for the library, which is actually a file named‘liblibrary.a’. The linker then uses this file as if it had beenspecified precisely by name.
 
The directories searched include several standard systemdirectories plus any that you specify with ‘-L’.
 
Normally the files found this way are library files—archivefiles whose members are object files. The linker handles an archivefile by scanning through it for members which define symbols thathave so far been referenced but not defined.
 
But if the file that is found is an ordinary object file, itis linked in the usual fashion. The only difference between usingan ‘-l’ option and specifying a file name is that ‘-l’ surroundslibrary with ‘lib’ and ‘.a’ and searches several directories.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python调用Windows下的DLL
vs2010中动态链接库的编写和调用
C#调用C/C++动态链接库(.dll)详解——第三篇 C++调用.dll
如何创建和使用dll及lib文件
VS2010中创建DLL图解
将 动态链接库文件添加到VC程序中
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服