首先需要安装好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
再次运行 ,就可以出来调用的结果了
本文详细介绍了如何在Eclipse中创建C/C++的.so动态库,包括解决编译时的-fPIC错误,以及如何在新工程中调用.so库,并处理运行时找不到库文件的问题。通过设置LD_LIBRARY_PATH环境变量或编译选项,成功运行调用动态库的程序。
9299

被折叠的 条评论
为什么被折叠?



