Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c_include_path vs ld_library_path

On either Ubunutu 12.04 or Springdale 6.4, using gcc and g++, what's the difference between C_INCLUDE_PATH (or CPLUS_INCLUDE_PATH) and LD_LIBRARY_PATH? Is the LD one only used at run-time, and the other two only at compile-time?

Since the INCLUDE and LIBRARY_PATH environment variables seem to be ignored by GCC on these operating systems, which should I set when constructing my ~/.bashrc file to make it as portable as possible (modulo changes in the actual paths) across modern Linux OSes?

like image 510
tsbertalan Avatar asked Aug 14 '13 20:08

tsbertalan


People also ask

What is the difference between path and LD_LIBRARY_PATH?

The PATH environment variable specifies the search paths for commands, while LD_LIBRARY_PATH specifies the search paths for shared libraries for the linker. The initial default values of PATH and LD_LIBRARY_PATH are specified in the buildfile before procnto is started.

What is LD_LIBRARY_PATH used for?

The LD_LIBRARY_PATH environment variable tells Linux applications, such as the JVM, where to find shared libraries when they are located in a different directory from the directory that is specified in the header section of the program.

What is C_include_path?

C_INCLUDE_PATH tells gcc where to search for C header files. CPLUS_INCLUDE_PATH tells gcc where to search for C++ header files. LIBRARY_PATH is used by gcc before compilation to find directories containing static and shared libraries to be linked.

What should be LD_LIBRARY_PATH?

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.


1 Answers

LD_LIBRARY_PATH is an environment variable which tells in which directories the dll loader should look for dynamic libraries when you start an executable. The variable is dangerous and deprecated

LIBRARY_PATH - tells linker where too look for libraries while building exe or lib INCLUDE_PATH - tells where to look for files referred in #include statements

In any case, LIBRARY_PATH and INCLUDE_PATH should be set in a particular build-system, not in bashrc. The easier a script can build c-sources, the more probable your PC may be infected with a rootkit.

BTW: gcc is a wrapper which invokes a proper compiler (e.g. cc or g++) and linker. g++ is gnu c++ compiler

EDIT Explanation, why LD_LIBRARY_PATH is dangerous.

I haven't used Linux for a couple of years and I wonder, that this env-variable is still in current distributions. It was considered as deprecated when I was using Linux (around 2006) as it provides very easy to exploit hook.

The problem with it is, it prescribes the order of path's in which ld.so - dynamic linker looks for required libraries. If LD_LIBRARY_PATH contained a writable directory, a hacker (in new speech a cybercriminal) could place in that directory a library with a name likely to be found in a system directory (e.g. /usr/lib). This library could first do any dirty job and then call the original library. Exploiting LD_LIBRARY_PATH is much easier then compromising binaries in system directories. And also such an exploit is hard to detect.

like image 173
Valentin H Avatar answered Oct 19 '22 12:10

Valentin H