eclipse 下 c/c++编译.so库,使用.so库(linux)

本文详细介绍了如何在Eclipse中创建C/C++的.so动态库,包括解决编译时的-fPIC错误,以及如何在新工程中调用.so库,并处理运行时找不到库文件的问题。通过设置LD_LIBRARY_PATH环境变量或编译选项,成功运行调用动态库的程序。

首先需要安装好eclipse 的cdt编译的环境。配置好gcc 编译环境。lunix 一般情况下都会有gcc ,windows就需要装个cygwin。

一、建立一个动态连库(.so文件)

1、先在eclipse中创建一个共享库工程

File->New->Project->C Project,选择Shared Library,选择empty project

2、创建工程的源文件share.cpp

File->New->Source Folder,指定名称为src

File->New->Source file,指定名称为share

最后的目录结构:



3、编写c代码以及c头文件


#include <stdio.h>
#include <stdlib.h>

void sayhello(){

	printf("hello !!1");

}

头文件:


void sayhello();

3、build project

会出现错误信息如下:

**** Build of configuration Debug for project share ****


make all 
Building file: ../src/share.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/share.d" -MT"src/share.d" -o "src/share.o" "../src/share.c"
Finished building: ../src/share.c
 
Building target: libshare.so
Invoking: GCC C Linker
gcc -shared -o "libshare.so"  ./src/share.o   
/usr/bin/ld: ./src/share.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
./src/share.o: could not read symbols: Bad value
collect2: ld 返回 1
make: *** [libshare.so] 错误 1


**** Build Finished ****

错误信息表示在编译时候需要 -fPIC 参数。

具体是这句缺少-fPIC参数

gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/share.d" -MT"src/share.d" -o "src/share.o" "../src/share.c"

需要加上-fPIC:

project->properties->c/c++ build -> setting->GCC C Compiler的commend中从原来的gcc 修改为gcc -fPIC

再次编译通过。信息如下:

**** Build of configuration Debug for project share ****


make all 
Building file: ../src/share.c
Invoking: GCC C Compiler
gcc -fPIC -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/share.d" -MT"src/share.d" -o "src/share.o" "../src/share.c"
Finished building: ../src/share.c
 
Building target: libshare.so
Invoking: GCC C Linker
gcc -shared -o "libshare.so"  ./src/share.o   
Finished building target: libshare.so
 


**** Build Finished ****


二、调用编译出来的.so文件。

1、先在eclipse中创建一个可执行工程

File->New->Project->C Project,选择Execuable Library,选择empty project

2、创建工程的源文件share.cpp

File->New->Source Folder,指定名称为src

File->New->Source file,指定名称为hello.c

3、复制share的头文件,因为需要引用shar.so的函数。编写hello.c文件


这里在编译的时候要引用到刚才建立的share.so共享库:

project->properties->c/c++ build -> setting->GCC C Linker 中的libraries引入库路径和库名称



不然会出现找不到函数问题如下:

**** Build of configuration Debug for project hello ****


make all 
Building target: hello
Invoking: GCC C Linker
gcc  -o "hello"  ./src/hello.o   
./src/hello.o: In function `main':
/ws/jack/Android/projects/testPorject/hello/Debug/../src/hello.c:11: undefined reference to `sayhello'
collect2: ld 返回 1
make: *** [hello] 错误 1


**** Build Finished ****


三、运行工程

出错!!!

/ws/jack/Android/projects/testPorject/hello/Debug/hello: error while loading shared libraries: libshare.so: cannot open shared object file: No such file or directory

找不到库文件。


原来lunix运行c/c++程序跑到默认路径去找这个share库文件了,但是肯定找不到。因为没把库放到默认路径里面。

这有两个办法指定运行时候库文件的路径:

一种,是设置系统的LD_LIBRARY_PATH 环境变量(在eclipse里设置对外部正式运行的程序无效)。

另外一种,是将动态链接库文件的路径编译进二进制可执行文件。-R+路径。


我的就利用LD_LIBRARY_PATH运行环境变量去设置路径了。设置成libshare.so的路径。

project->run as ->run configurations->Environment




参考:

http://blog.sina.com.cn/s/blog_7769660f01011pf1.html

http://hi.baidu.com/sfzhaoif/item/32ecf6bfe055b542bb0e1209

http://stackoverflow.com/questions/14448343/cannot-open-shared-object-file-no-such-file-or-directory-running-or-debugging

http://www.linuxidc.com/Linux/2012-08/68828.htm

再次运行 ,就可以出来调用的结果了





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值