C程序共享的库链接不设置LD_LIBRARY_PATH [英] C program linking with shared library without setting LD_LIBRARY_PATH
问题描述
我在读介绍GCC和它说,如果一个包有两种.A和.so。 GCC preFER的共享库。默认情况下,加载共享库只能在$ P $搜索pdefined设置系统目录,如的/ usr / local / lib目录
和的/ usr中/ lib目录
。如果库不位于这些目录之一,必须将它添加到负载路径,或者你需要使用 -static
选项强制它使用.A库。不过,我试过如下:
I was reading Introduction to GCC and it says if a package has both .a and .so. gcc prefer the shared library. By default the loader searches for shared libraries only in a predefined set of system directories, such as /usr/local/lib
and /usr/lib
. If the library is not located in one of these directories it must be added to the load path, or you need to use -static
option to force it to use the .a library. However, I tried the following:
VIM的hello.c:
vim hello.c:
#include <gmp.h>
#include <stdio.h>
int main() {
mpz_t x;
mpz_init(x);
return 0;
}
gcc hello.c -I/opt/include -L/opt/lib -lgmp (my gmp library is in opt)
./a.out
和它运行。书中说,它应该具备以下错误:
And it runs. The book says it should have the following error:
./a.out: error while loading shared libraries:
libgdbm.so.3: cannot open shared object file:
No such file or directory
(当然,书中使用GDBM为例但我用GMP,但是这没有关系吧?)
(well, the book uses GDBM as example but I used GMP, but this won't matter right?)
不过,我没有设置 LD_LIBRARY_PATH = /选择/ lib目录
,并且你可以看到我没有使用 -static
选项要么,但的a.out
仍然运行。
However, I did not set LD_LIBRARY_PATH=/opt/lib
, and as you can see I did not use -static
option either, but a.out
still runs.
你能都告诉我,为什么和告诉我怎么走在书中描述的错误?是的,我要的错误,所以我会明白我误解了。
Can you all tell me why and show me how to get the error described in the book? Yes I want the error so I will understand what I misunderstood.
推荐答案
从你的回应我的评论:
linux-gate.so.1 => (0xb7746000)
libgmp.so.10 => /usr/lib/i386-linux-gnu/libgmp.so.10 (0xb76c5000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7520000)
/lib/ld-linux.so.2 (0xb7747000)
所以,你的程序是从 / usr / lib目录
拿起库。
你能尝试做的是在你的的/ opt / lib中
,并连接到新的名称重命名库。
What you can try to do is rename the lib in your /opt/lib
, and link against the new name.
mv /opt/lib/libgmp.so /opt/lib/libgmp-test.so
gcc hello.c -I/opt/include -L/opt/lib -lgmp-test
然后尝试运行该程序。此外,比较 LDD
对结果的新的a.out
对你有什么了。
Then try running the program. Also, compare the result of ldd
against the new a.out
against what you got before.
这篇关于C程序共享的库链接不设置LD_LIBRARY_PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!