利用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形成了程序的两个部分,缺一不可。
运行一下:
利用C语言创建和使用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里面的函数
运行一下:
利用C语言创建和使用DLL文件

利用C语言创建和使用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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/351845.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vba判断文件编码格式_如何在VBA判断EXCEL或WORD文件已经打开,并用代码关闭

谢谢寻欢&#xff0c;原来核心就是GETOBJECT&#xff0c;特帖帮助内容&#xff0c;与大家分享&#xff1a;GetObject 函数示例该示例使用 GetObject 函数来获取对指定的 Microsoft Excel 的工作表 (MyXL) 的引用。它使用工作表的 Application 属性来显示或关闭 Microsoft Excel…

spring-service.xml 模板

ssm模板<?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xmlns:context"http://www.springframework.org/…

配置编译win7+VS2017+opencv4.0.1+contrib4.0.1

一、注意 1、opencv个各个版本并不是支持所有的VS版本&#xff0c;如opencv4.0.1支持vc14和vc15&#xff0c;而VS2013是vc12&#xff0c;配置起来会出错。 VS是一个集成开发环境&#xff0c;有不同的版本如VS2013&#xff0c;VS2015; vc是一个c的编译器&#xff0c;也有不同的…

scrapy立面parse_立面设计模式–设计观点

scrapy立面parse在上一篇文章中&#xff0c;我们描述了适配器设计模式 。 在今天的文章中&#xff0c;我们将介绍另一种类似的“四结构帮派”模式 。 顾名思义&#xff0c;结构模式用于从许多不同的对象形成更大的对象结构。 外观模式就是这样一种模式&#xff0c;它为系统内的…

[C++]在Visual Studio 2010中使用Google Test - 配置

我主要是想使用单元测试&#xff0c;VS2010是有自己的单元测试的&#xff0c;虽然我不抵触Microsoft的东西&#xff0c;但是自己做的非工业级的东西&#xff0c;去用Microsoft的解决方案是找罪受~所以使用了Google的测试方案。主要查阅了国外的一篇资料&#xff0c;虽然那位写得…

为什么传值时加号变成了空格_URL的参数中有加号传值变为空格的问题(URL特殊字符)...

1.URL特殊字符需转义2.空格换成加号()3.正斜杠(/)分隔目录和子目录4.问号(?)分隔URL和查询5.百分号(%)制定特殊字符6.#号指定书签7.&号分隔参数转义字符的原因&#xff1a;如果你的表单使用get方法提交&#xff0c;并且提交的参数中有“&”等特殊符的话&#xff0c;如…

LeetCode--single-number复杂度

1、题目 给定一个整数数组&#xff0c;每个元素都出现了两次&#xff0c;但有一个只出现了一次&#xff0c;请找出这个数。 Note&#xff1a;算法要求有线性时间复杂度&#xff0c;并且不占用额外的空间。 2、解法&#xff1a; public class Solution {public int singleNu…

Flask总结

Flask的优缺点 优点&#xff1a;Flask小而精&#xff0c;三方组件全 缺点&#xff1a;稳定性相对较差&#xff0c;三方组件版本问题&#xff0c;Flask一旦迭代&#xff0c;就可能造成三方组件不兼容的问题。 flask三剑客 小儿子 Django flask HTTPRespon…

交流设计

软件设计至关重要。 它是应用程序的基础。 就像一个蓝图&#xff0c;它为来自不同背景的聚会提供了一个通用平台。 它有助于理解&#xff0c;协作和发展。 设计不应仅视为开发的要素。 它不应该只存在于开发人员的头脑中&#xff0c;否则团队将发现它几乎无法增长&#xff0c;…

云顶之弈机器人法爆_LOL云顶之弈机器人出装怎么选

LOL云顶之弈有很多强力英雄&#xff0c;例如机器人正是当中之一。该棋子可以搭配多种阵容&#xff0c;因此装备选择非常重要。那么机器人怎么出装&#xff1f;下面就为大家带来LOL云顶之弈机器人出装推荐。LOL云顶之弈机器人出装怎么选7人口成型8人口上龙女&#xff0c;9人口千…

C/C++图形化编程(1)

归纳编程学习的感悟&#xff0c; 记录奋斗路上的点滴&#xff0c; 希望能帮到一样刻苦的你&#xff01; 如有不足欢迎指正&#xff01; 共同学习交流&#xff01; &#x1f30e;欢迎各位→点赞 &#x1f44d; 收藏⭐ 留言​&#x1f4dd; 信念是一把无坚不摧的利刃&#xff01…

在VS2010中使用Git【图文】

在之前的一片博客《Windows 下使用Git管理Github项目》中简单介绍了在Windows环境中使用Git管理Github项目&#xff0c;但是是使用命令行来进行操作的&#xff0c;本文将简单介绍下在VS2010中怎样使用Git&#xff0c;并来管理Github上的项目。 准备 安装Git命令行&#xff0c;…

Docker swarm 笔记

防火墙开放端口&#xff1a; TCP port 2377为集群管理通信TCP and UDP port 7946 为节点间通信UDP port 4789 为网络间流量创建attachable network docker network create --driveroverlay --attachable mynet-core 查看网络 docker network ls在manager1上创建swarm集群&#…

LeetCode-reverse integer复杂度

1、题目&#xff1a; Reverse digits of an integer. Example1: x 123, return 321Example2: x -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!…

juyter显示决策树图形_决策树分析细分市场

一&#xff0c;前言决策树就是利用图形来表述某项决策的本质&#xff0c;展现了各种选择和不确定因素之间的相互关系。决策树是一个利用像树一样的图形或决策模型的决策支持工具&#xff0c;包括随机事件结果&#xff0c;资源代价和实用性。它是一个算法显示的方法。决策树经常…

测试框架之GTest

gtest是google的一个C/C测试框架&#xff0c;由C实现&#xff0c;可在http://code.google.com/p/googletest/下载其源码及库文件。 gtest用法和cppunit用法差不多&#xff0c;个人比较习惯gtest&#xff0c;使用比cppunit方便些。主要通过宏TEST_F定义测试用例&#xff0c;通过…

idea使用junit测试_在JUnit测试中使用Builder模式

idea使用junit测试这并不是要成为技术含量很高的职位。 这篇文章的目的是为您提供一些指导&#xff0c;以使您的JUnit测试生活更加轻松&#xff0c;使您能够在几分钟内编写复杂的测试场景&#xff0c;并获得具有高度可读性的测试。 单元测试中有两个主要部分&#xff0c;需要编…

LeetCode-best time to buy and sell stock 2 数组

1、问题&#xff1a; Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock mu…

机器人酷跑闯关_机器人酷跑游戏下载-机器人酷跑手机版下载-地之图下载

机器人酷跑是一款机器人跑酷为游戏题材的动作冒险类游戏。在游戏中你将变成一个小机器人&#xff0c;你需要在各个平台上不断的进行跳跃&#xff0c;躲避各种障碍物以及陷阱&#xff0c;让你可以安全的到达终点。感兴趣的话就赶紧下载游戏玩玩吧&#xff01;机器人酷跑游戏简介…

Visual Studio属性配置中使用宏

在学习C语言的时候&#xff0c;我们曾经遇到过一个宏的概念。宏的作用机理本质上是宏的展开&#xff0c;C语言中的宏的用法也有很多种&#xff08;水其实很深...&#xff09;&#xff0c;不过从感觉上来讲&#xff0c;人们大致上会在以下的场景中&#xff0c;利用宏来解决一些窘…