简体   繁体   中英

How to find “my” lib directory?

I'm developing a C++ program under Linux. 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:

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

How do I find the lib directory? I tried to compute a relative part from argv[0] , but if /somewhere is in my PATH , argv[0] will just contain 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? What happens exactly if I supply the --prefix option to ./configure ?

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. 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

Assume your lib directory is "../lib" relative to executable

First you need to identify where myBin located, You can get it by reading /proc/self/exe

Then concat your binary file path with "../lib" will give you the lib directory.

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 .

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; 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:

$ 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?"...

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. 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)

The configure script relies on you to have the environment set up. 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. You might then use realpath etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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