Linux C library file and header file Problems

Source: Internet
Author: User
1. Start with several questions

Question 1: Undefined reference to 'xxx '.

Question 2:/usr/bin/ld: cannot find-LXXX.

Question 3: XXX. h: no such file or directory.

First of all, these issues are not compilation errors, but Link errors. If these errors occur, it indicates that your source program is normal, this is because your compilation options are incorrect or related library files or header files are missing. The first two problems are that the library file cannot be found, and the other problem is that the header file cannot be found. The following describes in detail the header files and library files.

2. database file-related (-L option and-L option)

After compilation, we enter the link stage. This involves the function library. For example, the printf function is usually used. We only include the "stdio. H ", this only contains the declaration of the function, but does not define the implementation of the function. So where is the implementation of the printf function? The answer is that in the function library, GCC links to a specific function library, where you can find the implementation of the printf function.

The-l parameter is used to specify the library to be linked by the program, and the-l parameter is followed by the database name. What is the relationship between the database name and the real library file name? Taking the Math Library as an example, his library name is m and his library file name is libm. so. It is easy to see that the library name is removed from the header lib and tail. So of the library file name.

Now we know how to get the database name. For example, we need to use a third-party library named libtest. so, we just need to put libtest. so copy to/usr/lib, add the-ltest parameter during compilation, and we can use libtest. so Library (of course libtest. so library function, we also need to work with libtest. so header file ).

Libraries placed in/lib and/usr/local/lib can be directly linked using the-l parameter, however, if the library file is not placed in these three directories but in other directories, the link will still fail if we only use the-l parameter. The error message is probably: "/usr/bin/ld: cannot find-LXXX", that is, the link program LD cannot find libxxx in the three directories. so, then another parameter-L is used. For example, the common X11 library is stored in the/usr/x11r6/lib directory, we need to use-L/usr/x11r6/lib during compilation.
-Lx11 parameter. The-l parameter is followed by the Directory Name of the library file. For example, if we put libtest. So in the/AAA/BBB/CCC directory, the Link parameter is-L/AAA/BBB/CCC-ltest.

3. header file (-I option)

-The I parameter is used to specify the header file directory. Generally, the/usr/include directory does not need to be specified. GCC knows where to find it, however, if the header file is not in/usr/include, We need to specify it with the-I parameter. For example, if the header file is placed in the/myinclude directory, the-I/myinclude parameter must be added to the compile command line, if this parameter is not added, you will get a "XXXX. h: No such file or directory "error. -The relative path can be used for the I parameter. For example, if the header file is in the current directory, you can use-I. to specify it.

 

Writing these compilation option parameters manually is generally troublesome and error-prone. For example, a function in the glib library is used in my program. Because the glib library is generally not a built-in library, I manually compile the source code for installation. The default installation path is/usr/local/lib. the header file is under/usr/local/include/glib-2.0. When I compile my own code, if I directly compile the code without adding any compilation options, the error "glib. h: no such file or directory" will be prompted. There are two solutions here.

Method 1: manually specify the installation path during the first installation and install it under/usr. By default, the installation is direct ". /configure "," make ", and" make install "commands. to manually specify the installation path, use ". /configure -- prefix =/usr "," make ", and" make install ".

Method 2: add the compilation option during compilation. I didn't directly use the-L and-I options here. I used a tool PKG-config. The specific compilation command is: "GCC 'pkg-config -- cflags -- libs glib-2.0 'test. c ".

PKG-config automatically generates the path of the library file and header file of the specified library. The usage of PKG-config is "PKG-config -- cflags -- libs pkgname", where pagname is the package name, if you do not know the specific package name, you can run the "PKG-config -- list-all" command to query all the development packages supported by the system.

(Sina Weibo: @ quanliang _ machine learning)

 

Related Article

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.