Linux C ++ LD_LIBRARY_PATH抑制标准库 [英] Linux C++ LD_LIBRARY_PATH suppressing standard libraries

查看:406
本文介绍了Linux C ++ LD_LIBRARY_PATH抑制标准库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C的新人,虽然我最近做了一些Objective C,所以有些它看起来很熟悉。



我正在写作一些测试程序来衡量某事是否可能。在我的(非常简单的Hello World)程序中,我使用cout输出一些文本,但是当我修改 LD_LIBRARY_PATH 指向一些库第三方应用程序,我将与之通信我没有输出,但没有编译器错误从g ++。



我试过包括标准路径例如 / usr / local / lib:/ usr / lib:/ lib 但是当我包含在路径中时,我还是没有输出。

  #include< iostream> 

using namespace std;

int main()
{
cout< 你好,世界;
return 0;
}

你可以看到它是一个很简单的程序,在我设置 LD_LIBRARY_PATH 之前。



我在任何阶段都没有得到任何错误,没有办法知道它是否正在运行。有什么我可以检查或任何方式,我来测试程序是否甚至运行?



我也尝试将输出写入设置路径之前工作的文件。



非常感谢



>

感谢回复(参见下面的结果评论)



根据@Nemo说的第三party app有它自己的libstdc ++版本,所以,这是正确的,我已经换出了他们的版本的标准安装版本的库。虽然我现在得到的'Hello World'输出我仍然收到大量的没有这样的文件或目录错误时strace'ing程序,所以我猜猜这个问题只有部分固定。

解决方案

如果您运行:

  ldd a.out 

其中a.out是可执行文件的名称,它将打印该可执行文件的运行时共享库​​依赖关系(参见 http://linux.die.net/man/1/ldd



这是一个输出示例:

  linux-vdso.so.1 => (0x00007fff463ff000)
libstdc ++。so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6(0x00007f75f6979000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6(0x00007f75f65ba000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6(0x00007f75f62bd000)
/lib64/ld-linux-x86-64.so.2(0x00007f75f6c85000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1(0x00007f75f60a7000)

知道你的应用程序正在搜索什么,你必须知道它正在LD缓存(或动态链接器缓存)中搜索它。这通常存储在/etc/ld.so.cache。但是,您不应手动编辑它。



您可以使用:

  ldconfig -p 

显示所有缓存条目。



当您运行

  ldconfig 
pre>

(以root身份),您将重新生成ldcache。 '/ lib'和'/ usr / lib'目录以及文件'/etc/ld.so.conf'中列出的任何目录都将被默认扫描,除非使用-n。



当调用可执行程序时,它还将检查LD_LIBRARY_PATH中的路径(除了ldcache之外)列出的任何文件

 <$ c 

$ c> ls -lah

您的可执行文件的ldd输出中列出的每个文件。如果任何文件丢失,您将必须用/ usr / lib或此目录中的相应共享库(* .so)替换它们,并重新生成ldcache。一旦缓存生成,它们应该显示在 ldconfig -p 输出中。



添加要包括在ldconfig ldcache生成中的新路径,可以将它们添加到/etc/ld.so.conf文件中。然后,下次生成ldcache时,它将在这些目录中搜索您的共享对象。您也可以将它们放在/ lib或/ usr / lib目录中。我建议不要这样做。相反,我建议使用/ usr / local / lib目录。它通常在ldcache路径中,并且用于与用户生成的共享库一起使用。



希望有助于



请参阅: http://linux.die.net/man/8/ldconfig



请参阅: http:// linux。 die.net/man/1/ldd



最后,locate命令对于在任何路径中查找文件都很有用。它使用一个经常更新的数据库,所以如果文件已经存在了一段时间,它可能在locate数据库。



您可以搜索像这样的文件:

 找到libstdc ++。so.6 

如果文件最近已添加到路径并且不在locate数据库中,则可以运行 updatedb 重新生成locate数据库。



有关这个主题的更多信息,我建议阅读这本书: http://www.network-theory.co.uk/docs/gccintro/


I'm kind of new to C++ although I have done some Objective C recently so some of it looks vaguely familiar.

I'm in the process of writing some test programs to gauge whether something is going to be possible or not. In my (very simple 'Hello World') program I'm outputting some text using cout which works fine, however when I modify the LD_LIBRARY_PATH to point to some libraries required by a 3rd party application that I'll be communicating with I get no output but no compiler errors from g++.

I've tried including the standard paths e.g. /usr/local/lib:/usr/lib:/lib but when I include this in the path I still get no output.

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World";
    return 0;
}

As you can see it a pretty simple program at the moment which works as expected before I set the LD_LIBRARY_PATH.

I'm not getting any errors at all at any stage so I've no way of knowing whether it's running. Is there anything I can check or any way for me to test whether the program is even running?

P.S. I've also tried writing output to a file which works before setting the path.

Many thanks

UPDATE

Thanks for the replies (see comment below for results)

Based on what @Nemo said about the 3rd party app having it's own version of libstdc++.so, which was correct, I've swapped out their version of that library with the standard install version. Although I am now getting the 'Hello World' output I am still receiving numerous No such file or directory errors when strace'ing the program so I'm guessing the issue is only partly fixed. I'm not sure if what I've done is 'allowed' or how to proceed from here.

解决方案

If you run:

ldd a.out

where a.out is the name of your executable, it will print the runtime shared library dependencies for that executable (see http://linux.die.net/man/1/ldd )

Here is an example output:

linux-vdso.so.1 =>  (0x00007fff463ff000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f75f6979000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f75f65ba000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f75f62bd000)
/lib64/ld-linux-x86-64.so.2 (0x00007f75f6c85000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f75f60a7000)

Now, once you know what your application is searching for, you have to know that it's searching for it in the LD cache (or, the dynamic linker cache). That is typically stored at /etc/ld.so.cache. However, you shouldn't edit it manually. Instead, you should use ldconfig to regenerate that cache.

You can use:

ldconfig -p

To show all of the cache entries.

When you run

ldconfig

(as root), you'll regenerate the ldcache. The '/lib' and '/usr/lib' directories, and any listed in the file '/etc/ld.so.conf' are scanned by default unless `-n' is used. Additional directories may be specified on the command line.

When an executable is invoked, it will also check the paths in LD_LIBRARY_PATH (in addition to the ldcache) for any files listed in the ldd output.

So, what you can do is run:

ls -lah

on each of the files listed in the ldd output for your exectuable. If any of the files are missing, you will have to replace them with the appropriate shared libraries (*.so) in the /usr/lib or such directory and regenerate the ldcache. Once the cache has been generated, they should show up in the ldconfig -p output.

If you want to add new paths to be included in the ldconfig ldcache generation, you can add them to the /etc/ld.so.conf file. Then, the next time that the ldcache is generated, it will search in those directories for your shared objects. You could also put them in /lib or /usr/lib directories. I'd recommend not doing that. Instead, I'd recommend using the /usr/local/lib directory. It's usually in the ldcache path and is intended to be used with user-generated shared libraries.

Hope that helps

See: http://linux.die.net/man/8/ldconfig

See: http://linux.die.net/man/1/ldd

Lastly, the "locate" command is useful for finding files in any path. It uses a database that is frequently updated, so if the file has been there for a while, it's probably in the locate database.

You can search for a file like this:

locate libstdc++.so.6

If the file has recently been added to a path and is not in the locate database, you can run updatedb to regenerate the locate database. Then, you should be able to find any file on your filesystem.

For more information surrounding this topic, I'd recommend reading this book: http://www.network-theory.co.uk/docs/gccintro/

这篇关于Linux C ++ LD_LIBRARY_PATH抑制标准库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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