Linux add environment variable with GCC compiler add include and LIB environment variables

Source: Internet
Author: User
Tags gtk

Add the following to the/etc/profile for all users effectively. Valid only for the current user in the home directory
Add the following to the. bashrc or. Bash_profile:
(Note: Do not add a space before the equal sign, or command not found may appear)

#在PATH中找到可执行文件程序的路径.
Export PATH = $PATH: $HOME/bin

#gcc找到头文件的路径
C_include_path=/usr/include/libxml2:/mylib
Export C_include_path

Path to header file found #g + +
Cplus_include_path= $CPLUS _include_path:/usr/include/libxml2:/mylib
Export Cplus_include_path

#找到动态链接库的路径
Ld_library_path= $LD _library_path:/mylib
Export Ld_library_path

#找到静态库的路径
Library_path= $LIBRARY _path:/mylib
Export Library_path


Library files are used when connecting (static libraries and shared libraries) and running (limited to programs that use shared libraries), and their search paths are set in the system. The general Linux system/lib and/usr/lib two directories as the default library search path, so using the libraries in these two directories does not need to set the search path can be used directly. For libraries that are outside the default library search path, you need to add the location of the library to the search path of the library. There are two ways to set the search path for a library file, which can be used as one of the following:

Specifies the search path for the library in the environment variable Ld_library_path.

Add a search path to the library in the/etc/ld.so.conf file.

It's a good idea to add the path of your own possible repository files to/etc/ld.so.conf.

Adding a method is also extremely simple, the absolute path of the library file is written directly into the OK, one line. For example:

/usr/x11r6/lib

/usr/local/lib

/opt/lib

It is important to note that the second search path is set up in a way that is sufficient for the location of the library (including shared libraries and static libraries) when the program is connected, but is not sufficient for the execution of programs that use shared libraries. This is because in order to speed up the location of shared libraries during program execution, and to avoid the inefficient use of search paths to find shared libraries, it is straightforward to read the library list file/etc/ld.so.cache from which to search. /etc/ld.so.cache is a non-text data file that cannot be edited directly, it is based on the search path set in/etc/ld.so.conf by/sbin/ldconfig command to set the shared library files under these search paths together (the Ldconfig command is executed with root permission). Therefore, in order to ensure the location of the library when the program executes, after setting the library search path in/etc/ld.so.conf, you must also run the/sbin/ldconfig command to update the/etc/ld.so.cache file. Ldconfig, simply put, it is the role of the/etc/ld.so.conf listed in the path of the library file cache to/etc/ld.so.cache for use. So when you have installed some library files, (for example, just installed glib), or modified ld.so.conf to add a new library path, you need to run the/sbin/ldconfig so that all library files are cached to Ld.so.cache, if not, even if the library file is clearly Usr/lib under, also will not be used, the results of the compilation process in the wrong, missing XXX library, to see the found clearly in that put, make want to scold computer stupid pig A.

When a program is connected, the search Paths for library files (static libraries and shared libraries) can be explicitly specified with the-l parameter in addition to the settings above. Because the path set with-L is prioritized, the path to the library to which you want to connect is typically specified in this way when you connect.

As explained earlier, there are two ways to set up a library search path: Set in environment variable Ld_library_path and in the/etc/ld.so.conf file. The second setting requires root permission to change the/etc/ld.so.conf file and execute the/sbin/ldconfig command. Also, when the system restarts, all GTK2-based programs will use the newly installed GTK + library at run time. Unfortunately, due to the changes in the GTK + version, this can sometimes be a compatibility issue for applications, causing some programs to run abnormally. To avoid these cases, the setting of the search path for the library during the installation of GTK + and its dependent libraries takes the first approach. This setting does not require root privileges and is simple to set up:

$ export ld_library_path=/opt/gtk/lib: $LD _library_path

You can use the following command to view the contents of the Ld_libray_path settings:

$ echo $LD _library_path

At this point, the two settings for the library are complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.