当前位置:首页 > 芯闻号 > 充电吧
[导读]有感于讲C语言的DLL文件的文章很少,自己查了半天,写了这么个非常简单的教程。自己也是摸C语言不久,依然感觉处于编程苦手的阶段。   1)为什么使用DLL文件   C语言复用代码有很多的形式,利用动态


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

int hello()
{
    printf ("Hello from DLLn");
}

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

#include <stdio.h>

int main()
{
    hello();
    hello();
    int a=2,b=3;
    int c;
    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 UseTestDll UseTestDll.c -L./ -lTestDll  这样就得到了 UseTestDll.exe 文件。UseTestDll.exe 和 TestDll.dll 形成了程序的两个部分,缺一不可。   运行一下:  

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

# 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 other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options used for compilation (‘-fpic’, '-fPIC’, or model suboptions) when you specify this linker option.1 

1 On some systems, ‘gcc -shared’ needs to build supplementary stub code for constructors to work. On multi-libbed systems, ‘gcc -shared’ must select the correct support libraries to link against. Failing to supply the correct flags may lead to subtle defects. Supplying them in cases where they are not necessary is innocuous

 


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


-o file  Write output to file. This is the same as specifying file as the second non-option argument to cpp. gcc has a different interpretation 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. The linking stage simplyis not done. The ultimate output is in the form of an object file for each source file. By default, the object file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, ‘.s’, etc., with ‘.o’. Unrecognized input files, not requiring compilation or assembly, are ignored.  


-llibrary-l library  Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.) It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. 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’, those functions may not be loaded. The linker searches a standard list of directories for the library, which is actually a file named ‘liblibrary.a’. The linker then uses this file as if it had been specified precisely by name.   The directories searched include several standard system directories plus any that you specify with ‘-L’.   Normally the files found this way are library files—archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined.   But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an ‘-l’ option and specifying a file name is that ‘-l’ surrounds library with ‘lib’ and ‘.a’ and searches several directories.

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

嵌入式开发作为信息技术领域的重要分支,其涉及的语言种类繁多,各具特色。这些语言的选择取决于目标平台的特性、性能需求、开发者的熟练程度以及项目的具体要求。本文将详细介绍几种常见的嵌入式开发语言,包括C语言、C++、汇编语言...

关键字: 嵌入式开发 C语言

Java语言和C语言是两种不同的编程语言,它们在语法、特性和应用领域上有许多差别。下面将详细介绍Java语言和C语言之间的差异以及它们各自的技术特点。

关键字: Java语言 C语言 编程

嵌入式系统是一种专门设计用于特定应用领域的计算机系统,它通常由硬件和软件组成,并且被嵌入到其他设备或系统中,以实现特定的功能。在嵌入式系统的开发过程中,选择适合的编程语言是至关重要的。C语言是一种被广泛应用于嵌入式系统开...

关键字: 嵌入式 计算机 C语言

C语言是一种广泛应用于软件开发领域的编程语言。它是由贝尔实验室的Dennis Ritchie在20世纪70年代初创建的,旨在为UNIX操作系统的开发提供一种高级编程语言。C语言具有简洁、高效、可移植性强等特点,因此成为了...

关键字: C语言 操作系统 应用程序

嵌入式系统是现代生活中无处不在的一部分。它们包括了我们的家电、汽车、智能手机、医疗设备等等。这些系统的工作必须高效、可靠,因为它们往往控制着生活中的关键方面。而C语言作为一种广泛用于嵌入式系统开发的编程语言,其质量和稳定...

关键字: 嵌入式系统 C语言 编程

在嵌入式系统开发领域中,C语言是使用最广泛的编程语言之一。它具有高效、灵活和可移植的特点,成为嵌入式系统设计师的首选语言。本文将介绍C语言编程的基本概念、特点以及在嵌入式系统开发中的应用。

关键字: 嵌入式系统 C语言 编程

C语言编译器是一种用于将C语言源代码转换为可执行程序的软件工具。它的主要功能是将C语言代码翻译成机器语言,以便计算机能够理解和执行。C语言编译器通常包括预处理器、编译器、汇编器和链接器等多个组件,它们协同工作以完成编译过...

关键字: C语言 编译器 Microsoft Visual C++

Matlab和C语言的区别是:1、用途不同;2、语法不同;3、运行速度不同;4、可移植性不同;5、代码管理不同。Matlab是一种数值计算和科学计算工具

关键字: matlab语言 C语言 系统编程

单片机是一种集成电路,它包含了中央处理器、存储器、输入输出接口和时钟等基本部件。单片机广泛应用于各种电子设备中,如家用电器、汽车电子、医疗设备等。单片机的使用领域已十分广泛,如智能仪表、实时工控、通讯设备、导航系统、家用...

关键字: 单片机编程 单片机 C语言

一直以来,嵌入式都是大家的关注焦点之一。因此针对大家的兴趣点所在,小编将为大家带来嵌入式的相关介绍,详细内容请看下文。

关键字: 嵌入式 C语言
关闭
关闭