有感于讲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.

利用C语言创建和使用DLL文件相关推荐

  1. c语言创建excel文件6,怎么利用c语言创建excel文件

    怎么利用c语言创建excel文件 想要利用c语言创建excel文件再把它导入excel其实很简单,如果数据简单的话,可以使用CSV(逗号分隔值)格式的文件.CSV格式的文件可以用OfficeExcel ...

  2. linux创建和编辑文件,linux基础-第九单元 利用vi编辑器创建和编辑正文文件

    vi编辑器简介 什么是vi vi编辑器的操作模式 vi编辑器的3种基本模式 在vi编辑器中光标的移动 移动光标位置的键与光标移动间的关系 进入插入模式 从命令行模式进入插入模式的命令 在命令行模式下删 ...

  3. 易语言如何调用c dll文件,易语言调用C++写的DLL

    直接调用会弹出堆栈错误的信息,原因是VS默认是__cdcel方式,而易语言是__stdcall,所以调用约定不一致导致堆栈错误. 解决方案很简单,易语言声明DLL函数时"在库中对应命令名&q ...

  4. 利用QT5.9创建并调用DLL(基于Visual Studio 2015)

    最近在做QT相关的项目,需要用到DLL,看了下网上的有关QT创建DLL的教程,大多数都是在QT Creator里面的.基于Visual Studio的非常少,这里就记录下自己走过的那些坑. 准备工作. ...

  5. C语言第五篇 python调用C语言写的动态链接库DLL文件

    学习目标:1.学会C语言写windows下的DLL文件.2.用gcc命令将C程序编译成DLL文件.3.学会用python调用C语言写的DLL. 学习内容1:先用C语言写一个简单程序,先建立dll.c文 ...

  6. 利用C语言创建文件并输入文件内容。

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define AS 200 char* ty(ch ...

  7. 利用go语言创建钱包并遍历钱包(wallet)

    基本知识 公钥加密算法使用的是成对的密钥:公钥和私钥,公钥可以公开,私钥不能被公开.比特币钱包实际上是一个密钥对,当你安装 一个钱包应用,或者是使用一个比特币客户端来生成一个新地址是,他就会为你生成一 ...

  8. c语言创建一个hello.txt文件,并检查是否成功,广东工业大学C语言验(上机)任务书2015版.doc...

    广东工业大学C语言验(上机)任务书2015版 广东工业大学实验(上机)任务书 (2015 - 2016 学年度第 1 学期) 课程名称 高级语言程序设计 课程设计 实验室名称及地点: 实验2号楼306 ...

  9. 利用go语言创建web server的两种方式

    相比于java/c#的mvc框架,go语言写web项目及其简单,创建一个web只需要简短的几行代码就可以实现功能: package mainimport "net/http"fun ...

最新文章

  1. 白领们注意啦:“过劳死”27个危险信号!
  2. INDEX 函数说明
  3. html5如何掷色子,html5 canvas掷骰子(简单,学习基础canvas)
  4. 从源码分析DEARGUI之全选和反选
  5. RID枚举工具RidEnum
  6. 【测试面试题】显示输入数字中的最大值
  7. 怎样填写个人简历较有效
  8. iOS开发之各种动画各种页面切面效果
  9. 【PAT】2021年冬季考试甲级,摸鱼游记、92分
  10. 从滴滴亏109亿说起
  11. 23.MySQL 函数
  12. 西门子cpu指示灯含义_CPU中的指示灯亮的意思
  13. html li去掉前面的小黑点 项目符号
  14. 芯片行业常用英文术语最详细总结(图文快速掌握)
  15. Python100例——第五章----不定方程的解
  16. 沁恒CH573开发板上手
  17. WPA3也不安全啦?WPA3-R3 H2E了解一下
  18. 云计算通俗解释,什么叫云计算
  19. BP神经网络原理简单介绍以及公式推导(矩阵形式和分量形式)
  20. 船舶AIS数据集/船舶历史航行数据集/AIS知识

热门文章

  1. c语言中组合函数,排列组合c怎么算 公式是什么
  2. linux 释放进程res_linux内存查看及释放
  3. 鲸鱼优化算法_盘点 35 个 Java 代码优化细节
  4. Profibus-DP光端机产品功能特点及技术参数详解
  5. [渝粤教育] 西南科技大学 基础工业工程 在线考试复习资料
  6. 【渝粤题库】陕西师范大学292301 国际金融学Ⅱ 作业(专升本)
  7. AC-DC电源模块设计原理及10个常见疑问
  8. android studio多屏幕,为多个屏幕实现多个布局但在android studio 2.1中无法工作
  9. centos 安装php扩展gd,linux(centos)下为php添加添加GD扩展
  10. unzip不能解压mysql的zip_Linux中zip压缩和unzip解压缩命令详解