Source code compilation explanation 1. In the process of compiling and installing software source code, configure must first execute./configure. What operations are performed in the background after./configure is executed? This step is generally used to generate Makefile to prepare for the next compilation. You can add parameters after configure to control the installation, such as the Code :. /configure-prefix =/usr indicates to install the software under/usr, And the execution file will be installed in/usr/bin (instead of/usr/local/bin ), the resource file will be installed in/usr/share (instead of the default/usr/local/share ). Generate the Makefile file required for compilation for the program Make to read and use, and then call the relevant Compilation Program (usually calling the Compilation Program is gcc) to compile the final binary program. When the configure script checks the corresponding dependent environment (for example, the software version and the corresponding library version), it usually uses the pkg-config tool to detect the corresponding dependent environment. Makefile is used for automatic compilation and linking. A project consists of many files. Changes to each file will lead to re-linking of the Project ----- but not all files need to be re-compiled, makefile records the file information and determines which files need to be re-compiled during the link! Pkg-config is used to retrieve information about the installed library files in the system. Typically, it is used for library compilation and connection. Generally, if the header file of the library is not in the/usr/include directory, you must use the-I parameter to specify the path during compilation. Because the same library may be located in different directories on different systems, users can install the library in different directories during library installation, even if the same library is used, because of the different library paths, the path of the header file specified by the-I parameter may be different from the path of the lib library specified by the-L parameter during connection, the result is that the compilation command interface is inconsistent. The compilation and connection inconsistency may cause problems when the same program is copied from one machine to another. Pkg-config is a tool used to solve the problem of inconsistent compiling connection interfaces. Its basic idea: pkg-config is A. pc file provided by the database to obtain various necessary information about the database, including version information, parameters required for compilation and connection. When needed, you can use the parameters (-cflags,-libs) provided by pkg-config to extract the required information for compilation and connection. In this way, no matter where the library file is installed, the. pc file corresponding to the Library can be accurately located, and the same compilation and connection commands can be used to unify the compilation and connection interfaces. It provides the following functions: <1> check the database version number. If the required library version does not meet the requirements, print the error message to avoid connecting to the library file of the wrong version. <2> obtain the compilation preprocessing parameters, such as macro definition and header file path. <3> obtain compilation parameters, such as the location of the database and other dependent libraries, file names, and other connection parameters. <4> automatically add the settings of other dependent libraries. For example, by default, the. pc file corresponding to each library that supports pkg-config is located in the lib/pkgconfig directory in the installation directory after installation. For example, if you have installed Glib in the/opt/gtk directory. pc file is/opt/gtk/lib/pkgconfig directory named glib-2.0.pc file: prefix =/opt/gtk/exec_prefix =$ {prefix} libdir =$ {exec_prefix}/libincludedir =$ {prefix}/include glib_gen1_al = glib-genmarshalgobject_query = gobject-queryglib_mkenums = glib-mkenums Name: GLibDescription: C Utility LibraryVersion: 2.12.13Libs:-L $ {libdir}-lglib-2.0Cflags:-I $ {includedir}/glib-2.0-I ${libdir}/glib-2. 0/shortdepkg-config-list-all lists all available packages in the/usr/lib/pkgconfig Directory, which contains various. pc files. The libname. pc file under/usr/local/lib/pkgconfig. The. pc file is usually installed in new software. You cannot create it yourself, and set the environment variable PKG_CONFIG_PATH to find the. pc file path. The-cflags parameter of pkg-config can be used to provide the required options during compilation, while the-libs parameter can provide the options during connection. For example, assume a sample. c program uses the Glib library, you can compile: $ gcc-c 'pkg-config -- cflagsglib-2.0 'sample. c and connect: $ gccsample. o-osample 'pkg-config -- libsglib-2.0 'or the above two steps can also be merged into the following step: $ gccsample. c-osample 'pkg-config -- cflags -- libsglib-2.0 'Can be seen: Because the pkg-config tool is used to obtain library options, No matter what directory the library is installed in, you can use the same compilation and connection commands to unify the compilation and connection interfaces. There are two basic prerequisites for extracting library compilation and connection parameters using the pkg-config tool: <1> the library itself must provide a corresponding. pc file during installation. The library description does not support the use of the pkg-config tool. <2> pkg-config must know where to find the. pc file. The PKG_CONFIG_PATH environment variable PKG_CONFIG_PATH is used to set the search path for the. pc file. pkg-config searches by path until the specified. pc file is found. In this way, the search path of the header file of the library is actually changed to the search path setting for the. pc file. After installing a required library, such as Glib. pc files, such as glib-2.0.pc copies to the/usr/lib/pkgconfig directory, the second is to add the search path for the glib-2.0.pc file by setting the environment variable PKG_CONFIG_PATH. Add the environment variable PKG_CONFIG_PATH. In bash, perform the following settings: $ exportPKG_CONFIG_PATH =/opt/gtk/lib/pkgconfig: $ PKG_CONFIG_PATH, when using other programs or libraries of the Glib library, pkg-config knows to first find the glib-2.0.pc in the/opt/gtk/lib/pkgconfig directory (GTK + and other. pc files will also be copied here, and the corresponding files will be searched here first. pc files ). Then, you can use pkg-config to extract the library compilation and connection parameters for the program to compile and connect. Note that the environment variable setting is only valid for the current terminal window. If there is a terminal window without the above settings, pkg-config will not find the newly installed glib-2.0.pc file, which may cause subsequent installation (such as installation of Atk after Glib) unable to proceed. If you want it to take effect permanently, you can write the environment variable to/etc/profile. 2. make is to compile. Most source code packages are compiled in this step (of course, some software written in perl or python needs to call perl or python for compilation ). If an error occurs during the make process, you need to write down the error code (note that it is not just the last line), and then you can submit the bugreport to the developer (generally there is a submission address in INSTALL ), or your system has less dependent libraries, and you need to carefully study the error code. Make is used to start source code compilation and provide some functions. These functions are provided by the Makefile setting file. For example, make install generally indicates installation, make uninstal is uninstallation. If no parameter is added, the source code is compiled by default. Make is a control program automatically compiled in the Linux development kit. It uses the compilation specifications written in Makefile to automatically call gcc, ld, and run programs that need to be compiled. Generally, the Makefile control code is generated by the configure configuration script based on the given parameters and system environment. 3. make install (of course, some software must first run make check or make test for some tests ), this step usually requires you to have the root permission (because you want to write files to the system) to upgrade glibc. When compiling and installing some software, we will prompt you to upgrade glibc, glibc was upgraded through source code compilation, but the same error was reported. Why? Because you upgrade the source code to compile and install glibc, When you install the software, the system does not find the corresponding library of glibc when checking the dependent environment, in this case, you need to manually specify the library for the system to find glibc. If you install glibc in/opt/glibc, you can directly set the pkg-config-puth environment variable. $ Export =/opt/glibc/lib/pkgconfig. Continue to install your software!