Getting started with function libraries

Source: Internet
Author: User

When learning a function library, the first thing we often do is to download the source code of the function library for compilation and installation (usually in these three steps :. /configure-> make install), and then write some small use cases that call the library function to see if it can run normally and the effect after running. After installing library functions, we often encounter the following problems when we are eager to use GCC for case Compilation:

 
Test. C: X: XX: Fatal error: XXXX. h: no such file or directorycompilation terminated.

 

Or:

 

 
Test. c :(. Text + 0xx): Undefined reference to 'xxxx' collect2: LD returned 1 exit status

 

 

After encountering such a problem, some students fail to find a solution for the problem for several hours, so they are afraid of difficulties and finally fall to the entry threshold of library function learning.

 

The following describes the cause of the problem and provides a solution based on the glib function library instance to guide beginners to get started with the function library step by step (this articleArticleC/C ++ source code is used as the basis for discussion. The default editor is GCC and CC under GNU ).

 

1. Cause Analysis

First, analyze the causes of the above problems. First, we need to understand this knowledge: Compilation and linking principles, and find the path for GCC header files and library files.

 

ProgramCompilation and link: During program compilation, the compiler checks whether the syntax is correct and the declaration of functions and variables. This requires us to inform the compiler of the header file of the function and variable declaration. After the syntax is correct and the paths of the relevant header files are given to the compiler, the compiler will compile the intermediate target file (. O or. OBJ file ).

 

When linking a program, the linker searches for the function implementation in all intermediate target files or library files (which are packaged by intermediate target files). If the linker does not find the function implementation, it returns the link failure message.

 

After knowing this, check the error messages listed at the beginning of the article. The cause of the error is obvious. The first error occurs when a program does not find a header file during compilation. The second error occurs because the linker does not find the library file on which a function depends or the function is not implemented at all.

 

Where can GCC find header files and library files? GCC has its
The default search path. The first file is located in/usr/include,/usr/local/include, and other directories. during compilation, you can use the-I option of GCC to specify the header file.
The directory where the library files are located, such as/usr/lib and/usr/local/lib, during compilation, you can use the-L option of GCC to specify the directory for searching library files.

 

Obviously, the header file/library file in the above error message is not in the GCC default header file/library file search path. Where have they been installed?

 

2. Solution

By viewing the makefile in the source code file of the function library, we can know the header file of the function library and the path for installing the library file. Taking the glib function library as an example, Here we only look at its makefile (. /configure command generation.

Prefix =/usr/localexec_prefix =$ {prefix} includedir =$ {prefix}/includelibdir =$ {exec_prefix}/lib

These values specify the installation path of the header file/library file of the glib function library. After the make install command is executed, the header file and library file will be installed in the specified path respectively.

 

In addition, we can use the locate command to locate the path of the header file and library file. For example, the complete path of the file name containing the "Glib" field will be listed, then, locate the path of the library file and header file.

 

Suppose we have installed the glib library from the source code, and we try to run a hello word applet to verify the installation result.

 

 
# Include <stdio. h> # include <glib. h> int main (void) {g_printf ("Hello world! \ N "); Return 0 ;}

 

Run:

Gcc-I/usr/local/include/glib-2.0/-I/usr/local/lib/glib-2.0/include/-lglib-2.0-O hello. c

 


Because the glib library file has been installed in the GCC default Library File Link directory (/usr/local/lib/), no library file search path is specified here.

 

Try to execute:

 
Gcc-O hello. CGCC-I/usr/local/include/glib-2.0/-I/usr/local/lib/glib-2.0/include/-O hello. c

See what results are available :)

 

Another tool that can help us solve the Path Problem of the header file or library file is PKG-config, many functions
The Library supports the PKG-config command. In the makefile of glib, this statement is available: pkgconfigdir =
$ (Libdir)/pkgconfig, which indicates the. PC file (that is, the glib-2.0.pc) installation path for glib. Let's look at the glib-2.0.pc File
Which of the following are the main contents:

Prefix =/usr/localexec_prefix =$ {prefix} libdir =$ {exec_prefix}/libincludedir =$ {prefix}/includelibs:-L $ {libdir}-lglib-2.0Cflags: -I $ {includedir}/glib-2.0-I $ {libdir}/glib-2.0/include

 

Cflags provides the required options for compiling, Libs provides the required options for linking, and then uses the PKG-config command as follows:

 

 
GCC 'pkg-config -- cflags -- libs glib-2.0 '-O hello. c

 

 

If you know the cause and solution of the problem, you will encounter "No such file or directory" or "undefined reference to 'xxxx'" during the next compilation?

 

This article references the following article: "write makefile With Me" by Chen Hao

makefile helper: pkgconfig by Li xianjing

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.