gcc编译dll和调用dll

方法一:

 

共有三个文件:print.h,print.c,test.c

 

***************************************************************
print.h: 文件内容

 

#ifndef PRINT_H
#define PRINT_H

 

#ifdef __cplusplus
extern " C " {
#endif

//打印点东西
void Print(int iNum);


#ifdef __cplusplus
}
#endif

 

#endif

 

***************************************************************

 

 

 

***************************************************************

print.c: 文件内容

 

#include <stdio.h>
#include "print.h"

void Print(int iNum)
{
switch(iNum)
{
case 1:
printf("hello,this is 1/n");
break;
case 2:
printf("ok ,2 now/n");
break;
default:
printf("hihi,default now /n");
break;
}
getch();
return;
}

 

***************************************************************

 

***************************************************************

test.c:文件内容

 

#include <stdio.h>
#include "print.h"

int main()
{
printf("please print a num:/n");
int iNum = -1;
scanf("%d", &iNum);
Print(iNum);
return 1;
}

 

 ***************************************************************

 

编译动态dll库:

gcc -Wall -shared print.c -o print.dll

或者

gcc --share print.c -o print.dll

调用dll库生成exe文件:

gcc test.c print.dll -o test

 

 

 编译静态库,可供windows调用:

1、gcc -shared -o print.dll print.c -Wl,--output-def,print.def,--out-implib,libprint.a

2、lib /machine:i386 /def:print.def

 

调用:vs2005

main.c

 

#include <stdio.h>
#include <stdlib.h>
#include "print.h"
#pragma comment(lib,"print.lib")

int main()
{
    Print(1);
 system("pause");
   
    return 0;
}

 

 

 方法二:

两个文件:Foo.c 和 Foo_test.c

 

***************************************************************

Foo.c:文件内容

 

#include <windows.h>
#include <stdio.h>

// 这就是按需加载的dll的主函数,dll被加载、卸载时,系统都回调用这个函数,通过dwReason判断
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpvReserved)
{
        switch(dwReason) {
        // 如果是进程加载本dll
        case DLL_PROCESS_ATTACH:
                printf("process attach/n");
                break;
        // 如果是进程卸载本dll
        case DLL_PROCESS_DETACH:
                printf("thread attach/n");
                break;
        // 如果是线程加载本dll
        case DLL_THREAD_ATTACH:
                printf("thread attach/n");
                break;
        // 如果是线程卸载本dll
        case DLL_THREAD_DETACH:
                printf("process attach/n");
                break;
        }
        // 如果返回FALSE,则说明加载失败,不会继续被加载,也不可使用
        return TRUE;
}

int foo(char *str)
{
        printf("%s/n", str);
        return 0;
}

 

***************************************************************

 

 

***************************************************************

Foo_test.c:文件内容

#include <windows.h>
#include <stdio.h>

typedef int (*FOO)(char *str);

int main()
{
        HMODULE mod;
        FOO     foo;

        mod = LoadLibrary("lib.dll");

        printf("from main/n");

        if((foo = (FOO)GetProcAddress(mod, "foo")) != NULL) {
                foo("hello world");
        }

        FreeLibrary(mod);

 getch();

        return 0;
}

 

***************************************************************

 

编译动态dll库:

gcc -Wall -shared Foo.c -o Foo.dll

或者

gcc --share Foo.c -o Foo.dll

 

调用dll库生成exe文件:

gcc Foo_test.c Foo.dll -o Foo_test

 

 

资料:

1、基于MSYS的Win32动态链接库DLL开发:http://code.google.com/p/msys-cn/wiki/Chapter5

 

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
将 C 类编译DLL 可以让我们在 Python 中调用 C 类的功能。以下是将 C 类编译DLL 并在 Python 中调用的基本步骤: 1. 创建一个 C 源文件,其中包含需要导出到 DLL 的函数和类。确保这些函数和类在 C 代码中正确实现并编译成功。 2. 在 C 代码中使用适当的编译指令(如 `__declspec(dllexport)`)来指定需要导出到 DLL 的函数和类。这将告诉编译器将这些函数和类添加到 DLL 的导出表中。 3. 使用 C 编译器将源文件编译DLL。这可以通过命令行中的编译命令完成,例如使用 gcc 命令将源文件编译DLL。 4. 在 Python 中使用 ctypes 模块加载 DLL,并定义需要调用的函数原型。ctypes 模块允许 Python 使用动态链接库中的函数。 5. 在 Python 中实例化 C 类,使用先前定义的函数原型调用 DLL 中的函数。确保按照正确的参数类型和顺序来调用函数。 下面是一个简单的示例代码: C 代码(example.c): ```c #include <stdio.h> __declspec(dllexport) int add(int a, int b) { return a + b; } __declspec(dllexport) void greet() { printf("Hello from C DLL!\n"); } ``` Python 代码: ```python import ctypes # 加载 DLL dll = ctypes.CDLL('example.dll') # 定义函数原型 add_func = dll.add add_func.argtypes = (ctypes.c_int, ctypes.c_int) add_func.restype = ctypes.c_int greet_func = dll.greet greet_func.restype = ctypes.c_void_p # 调用 DLL 中的函数 result = add_func(3, 4) print("Result:", result) greet_func() ``` 请注意,以上步骤仅提供了基本的示例,实际情况可能因 C 代码的复杂性而有所不同。在编译调用 DLL 的过程中可能会遇到各种问题,例如编译器和操作系统的差异,因此请根据特定的环境和需求进行适当的调整和调试。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值