参考文章: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所在目录