cygwin 下使用gcc创建和使用动态库

9 篇文章 0 订阅

参考文章:http://www.linuxidc.com/Linux/2011-02/32453.htm
以及讨论帖:http://bbs.chinaunix.net/viewthread.php?tid=3555666

摘要:
Cygwin下gcc只能使用.dll动态库,不能使用.so类型的动态库,所有在用gcc创建动态库的时候,-o的参数指定的动态库的名字需要为libxxx.dll。

具体实例:

// so_test.h

#include "stdio.h"

void test_a();
// test_a.c

#include "so_test.h"

void test_a()
{
	printf("this is in test_a...\n");
}
$ gcc test_a.c -fPIC -shared -o libtest.dll
// test.c

#include "so_test.h"

int main()
{
	test_a();

	return 0;
}
$ gcc test.c -L. -ltest -o test

----------------------------
参数说明:
-fPIC:示生成地址无关代码,
-shared:指示生成动态链接库
-L:表示要连接的库在当前目录中,
-ltest:编译器查找动态连接库时有隐含的命名规则,即在给出的名字前面加上lib,后面加上.dll(Cygwin)( Linux环境中是.so)来确定库的名称

运行效果:
$ ./test.exe
this is in test_a...

注意:
最后要注意的问题是,该test程序时,可能会提示 error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
解决方法:指定LD_LIBRARY_PATH为libtest.dll所在目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值