Take the 2-minute tour ×

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:


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.

share|improve this question
If your library is static, just add -L your/lib/dir; if it is dynamic, you could have an LD_LIBRARY_PATH, etc... –  Basile Starynkevitch Mar 24 '12 at 11:59
As I said, the library is an LLVM Bitcode file, so these won't help. Just think of it as an image or a text file I want to read from my program. –  Thomas Schaub Mar 24 '12 at 12:29
add comment

6 Answers

maybe what you want is :


unix directory reference: http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilestruct.html

share|improve this answer
Yes and know. Ideally I want to be able to move the program whereever I want, so that /usr/lib is fine as long as the binary is in /usr/bin. My program is going to be integrated into the development environment of a chair at my university and they use different paths for e.g. release and debug binaries. –  Thomas Schaub Mar 24 '12 at 12:13
add comment

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.

share|improve this answer
This should work, but I try to avoid platform dependent code in general. –  Thomas Schaub Mar 24 '12 at 12:34
add comment

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
share|improve this answer
This seems reasonable. Now I only have to find out how to convert your example to scons. –  Thomas Schaub Mar 24 '12 at 12:32
add comment



LD_LIBRARY_PATH is handy for development and testing:

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

[read more]

share|improve this answer
The library is LLVM Bitcode so I can't use that. –  Thomas Schaub Mar 24 '12 at 12:31
add comment

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.

share|improve this answer
add comment

You could use the readlink system call on /proc/self/exe to get the path of your executable. You might then use realpath etc.

share|improve this answer
add comment

Your Answer


