简体   繁体   English

如何找到“我的” lib目录?

[英]How to find “my” lib directory?

I'm developing a C++ program under Linux. 我正在Linux下开发一个C ++程序。 I want to put some stuff (to be specific, LLVM bitcode files, but that's not important) in libraries, so I want the following directory structure: 我想在库中放入一些东西(具体来说是LLVM位代码文件,但这并不重要),所以我想要以下目录结构:

/somewhere/bin/myBin
/somewhere/lib/myLib.bc

How do I find the lib directory? 如何找到lib目录? I tried to compute a relative part from argv[0] , but if /somewhere is in my PATH , argv[0] will just contain myBin . 我尝试从argv[0]计算相对部分,但是如果/somewhere在我的PATH ,则argv[0]仅包含myBin Is there some way to get this path? 有什么办法可以走这条路吗? Or do I have to set it at compile time? 还是我必须在编译时设置它?

How do GNU autotools deal with this? GNU自动工具如何处理呢? What happens exactly if I supply the --prefix option to ./configure ? 如果为./configure提供--prefix选项,会发生什么情况?

Edit: The word library is a bit misleading in my case. 编辑: 字库是有点在我的情况产生误导。 My library consist of LLVM bitcode, so it's not an actual (shared) object file, just a file I want to open from my program. 我的包含LLVM位代码,因此它不是实际的(共享)对象文件,而只是我想从程序中打开的文件。 You can think of it as an image or text file. 您可以将其视为图像或文本文件。

maybe what you want is : 也许您想要的是:

/usr/lib 

unix directory reference: http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilestruct.html Unix目录参考: http : //www.comptechdoc.org/os/linux/usersguide/linux_ugfilestruct.html

Assume your lib directory is "../lib" relative to executable 假设您的lib目录是相对于可执行文件的“ ../lib”

First you need to identify where myBin located, You can get it by reading /proc/self/exe 首先,您需要确定myBin的位置,可以通过阅读/ proc / self / exe来获取它。

Then concat your binary file path with "../lib" will give you the lib directory. 然后,用“ ../lib”连接二进制文件路径将给您lib目录。

You will have to use a compiler flag to tell the program. 您将必须使用编译器标志来告知程序。 For example, if you have a plugin dir: 例如,如果您有一个插件目录:

# Makefile.am
AM_CPPFLAGS = -DPLUGIN_DIR=\"${pkglibdir}\"
bin_PROGRAMS = awesome_prog
pkglib_LTLIBRARIES = someplugin.la

The list of directories to be searched is stored in the file /etc/ld.so.conf . 要搜索的目录列表存储在文件/etc/ld.so.conf

In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories; 在Linux中,环境变量LD_LIBRARY_PATH是用冒号分隔的一组目录,在该目录中,应在标准目录集之前先搜索库; this is useful when debugging a new library or using a nonstandard library for special purposes. 这在调试新库或出于特殊目的使用非标准库时很有用。

LD_LIBRARY_PATH is handy for development and testing: LD_LIBRARY_PATH适用于开发和测试:

$ export LD_LIBRARY_PATH=/path/to/mylib.so
$ ./myprogram

[read more] [阅读更多]

Addressing only the portion of the question "how to GNU autotools deal with this?"... 仅解决部分问题“如何使用GNU自动工具处理此问题?” ...

When you assign a --prefix to configure, basically two things happen: 1) it instructs the build system that everything is to be installed in ${prefix}, and 2) it looks in ${prefix}/share/config.site for any additional information about how the system is set up (it is common for that file not to exist.) It does absolutely nothing to help find libraries, but depends on the user having set up the tool chain properly. 当您分配--prefix进行配置时,基本上会发生两件事:1)它指示构建系统将所有内容都安装在$ {prefix}中,以及2)它看起来在$ {prefix} /share/config.site中有关如何设置系统的任何其他信息(通常不存在该文件。)它绝对无助于查找库,但取决于用户是否正确设置了工具链。 If you want to use a library in /foo/lib, you must have your toolchain set up to look there (eg, by putting /foo/lib in /etc/ld.so.conf, or by putting -L/foo/lib in LDFLAGS and "/foo/lib" in LD_LIBRARY_PATH) 如果要在/ foo / lib中使用库,则必须在其中设置工具链(例如,通过将/ foo / lib放在/etc/ld.so.conf中,或通过-L / foo / LDFLAGS中的lib和LD_LIBRARY_PATH中的“ / foo / lib”)

The configure script relies on you to have the environment set up. configure脚本依赖您来设置环境。 It does not help you set up that environment, but does help by alerting you that you have not done so. 它不会帮助您设置该环境,但会通过提醒您尚未设置环境来提供帮助。

You could use the readlink system call on /proc/self/exe to get the path of your executable. 您可以在/proc/self/exe上使用readlink系统调用来获取可执行文件的路径。 You might then use realpath etc. 然后,您可以使用realpath等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM