利用C语言创建和使用DLL文件

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)
{
    int c;
    c=a+b;
    return c;
}
 
然后写一个主程序来调用,文件名为 UseTestDll.c
//UseTestDLL.c 

#include <</font>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 形成了程序的两个部分,缺一不可。
运行一下:
利用C语言创建和使用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里面的函数
运行一下:
利用C语言创建和使用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.
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: VS Code是一款非常流行的集成开发环境(IDE),它支持多种编程语言,包括C语言。在VS Code中编写C语言DLL(动态链接库)非常简便。下面是一些基本步骤: 1. 安装VS Code和C/C++扩展:首先,确保你已经成功安装了VS Code,并在扩展市场搜索并安装C/C++扩展。这个扩展提供了C语言编写所需的各种功能和工具。 2. 创建C文件:在VS Code中,使用Ctrl+N或者点击"文件"->"新建文件"来创建一个新的C文件。将其保存为.c文件,例如"mydll.c"。 3. 编写C代码:在新创建的C文件中,开始编写你的C代码。这里你可以编写DLL所需的任何函数和逻辑。确保你按照C语言的语法规则来编写代码。 4. 生成DLL:完成代码编写后,可以使用某些编译器或者GCC(GNU编译器集合)来生成DLL文件。如果你已经在系统上安装了GCC,可以在VS Code的终端中使用相应的命令行,例如"gcc -shared -o mydll.dll mydll.c"来生成DLL文件。 5. 调试代码:VS Code还提供了强大的调试功能。你可以使用调试器来逐步调试你的DLL代码,以发现和解决潜在的问题。 6. 示例代码: ```c #include <stdio.h> #include <windows.h> // DLL的导出函数 __declspec(dllexport) int add(int a, int b) { return a + b; } // DLL的入口函数 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH: // 在DLL加载时执行的代码 break; case DLL_PROCESS_DETACH: // 在DLL卸载时执行的代码 break; case DLL_THREAD_ATTACH: // 在新线程创建时执行的代码 break; case DLL_THREAD_DETACH: // 在线程终止时执行的代码 break; } return TRUE; } ``` 总之,使用VS Code编写C语言DLL非常方便。只需安装相应的扩展,创建编写C代码,并使用编译器生成DLL文件即可。 ### 回答2: 在VsCode中编写C语言DLL(动态链接库),首先需要安装相关的插件来支持C语言的开发。我们可以使用"C/C++"插件和"Code Runner"插件。 步骤如下: 1. 在VsCode的扩展栏中搜索并安装"C/C++"插件和"Code Runner"插件。 2. 打开一个新的文件夹作为你的工作目录,点击“文件”然后选择“打开文件夹”,选择你的工作目录。 3. 在工作目录中创建一个名为`main.c`的文件,用于编写你的C代码。 4. 在`main.c`文件编写你需要导出的函数。通过使用`__declspec(dllexport)`关键字,你可以指定需要导出的函数。例如,编写一个简单的示例函数如下: ```c #include <stdio.h> __declspec(dllexport) int add(int a, int b) { return a + b; } ``` 5. 保存`main.c`文件。 6. 打开“终端”窗口(快捷键是 `Ctrl + `),运行以下命令来编译生成DLL文件: ``` gcc -shared -o mydll.dll main.c ``` 这将在工作目录中生成一个名为`mydll.dll`的DLL文件。 7. 将生成的DLL文件放置在你需要使用它的项目目录中。 现在,你可以在其他C语言项目中使用DLL文件。只需在你的C代码中包含该DLL的头文件,并调用其中的函数即可。例如,在另一个C代码文件中,可以这样使用DLL的函数: ```c #include <stdio.h> int main() { // 调用DLL中的函数 int result = add(2, 3); printf("Result: %d\n", result); return 0; } ``` 以上就是使用VsCode编写C语言DLL的基本步骤。你可以根据需要编写更复杂的代码,并对DLL进行更多的配置和优化。希望对你有所帮助! ### 回答3: VS Code 是一款功能强大的文本编辑器,它支持多种编程语言,包括 C 语言。在 VS Code 中编写 C 语言 DLL(动态链接库)可以通过以下步骤完成: 1. 安装 C/C++ 扩展:在 VS Code 中点击左侧的扩展图标,搜索并安装名为 "C/C++" 的扩展。该扩展提供了对 C 语言的语法高亮、代码智能提示、代码格式化等功能。 2. 创建工作区:在 VS Code 的菜单栏中选择 "文件" -> "打开文件夹",选择一个文件夹作为工作区。在该文件夹下创建一个新的文件夹,作为 C DLL 项目的根目录。 3. 创建 C 文件:在项目根目录下创建一个以 ".c" 为扩展名的 C 文件。这个文件将包含 DLL 的源代码。 4. 编写代码:在 C 文件编写 DLL 的代码。DLL 是一个动态链接库,可以被其他程序调用。代码要包含合适的函数和导出声明,以供其他程序使用。可以使用 C 的标准库函数和其他自定义函数。 5. 配置编译任务:在 VS Code 的菜单栏中选择 "查看" -> "终端",打开终端窗口。在终端中运行 "gcc"(或其他 C 语言编译器)命令编译 DLL 代码。可以按照编译器的要求指定输出文件名、编译参数等。 6. 编译并生成 DLL:在终端中输入命令并执行,将代码编译为 DLL 文件。如果没有错误,将在项目根目录生成 DLL 文件,可以通过其他程序调用该 DLL。 完成以上步骤后,就可以使用 VS Code 编写并生成 C 语言 DLL。注意,这里只是提供了基本的步骤,具体的代码实现和编译参数需要根据具体需求和开发环境进行调整。在编写和编译过程中可能会出现错误,可以通过调试和查阅相关文档来解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值