我如何链接C中的共享对象? [英] how can I link a shared object in C?

查看:115
本文介绍了我如何链接C中的共享对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个使用共享对象的简单程序,用 dlopen()打开它。我也编译并链接了如下的共享对象:

  gcc -o libmylib.so libmylib.c -shared -fPIC -Wall 

gcc -o program program.c -L。 -lmylib -ldl -Wall

当我第一次尝试运行该程序时,

 无法打开libmylib.so:没有这样的文件或目录

所以我搜索了互联网,发现我必须将我的共享对象复制到 / lib / i386-linux-gnu / 以便程序运行。所以我这样做了,并且它工作了,但是我试图用其他方式来完成它,因此我从 / lib / lib中删除了 libmylib.so i3686-linux-gnu / 目录。现在,当我试图运行程序时,它显示没有错误,但一直说分段错误。很显然,共享对象无处可寻,但我如何链接它而不需要复制任何东西?



我使用Ubuntu 11.10

$ b

解决方案

当你编译程序时,你正确地链接了它;使用 -L。 -lmylib 。但是,如果它不是标准的系统库,执行环境需要知道在哪里查找它。一种方法是使用环境变量 LD_LIBRARY_PATH ,如下所示:

  LD_LIBRARY_PATH =。 ./program 

  export LD_LIBRARY_PATH =。 
./program


I made a simple program that uses a shared object, opening it with dlopen(). I also compiled and linked the shared object like below:

gcc -o libmylib.so libmylib.c -shared -fPIC -Wall

gcc -o program program.c -L. -lmylib -ldl -Wall

When I tried to run the program for the first time it said something like

cannot open libmylib.so: no such file or directory

so I searched the internet and found that I have to copy my shared object to /lib/i386-linux-gnu/ in order for the program to run. So I did so, and it worked, but then I tried to do it in other ways and therefore I removed libmylib.so from the /lib/i3686-linux-gnu/ directory. Now when I'm trying to run the program it shown no errors but keeps saying Segmentation fault. It is clear that the shared object is nowhere to be found, but how can I link it without copying anything?

I am using Ubuntu 11.10

解决方案

When you compiled the program, you linked it properly; using -L. -lmylib. However, if it's not a standard system library, the execution environment needs to know where to look for it. One way you can do this is by using the environment variable LD_LIBRARY_PATH, like so:

LD_LIBRARY_PATH=. ./program

or

export LD_LIBRARY_PATH=.
./program

这篇关于我如何链接C中的共享对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆