Some usages of pkg-config

Source: Internet
Author: User
Tags gtk

When we use a third-party library, we often use the Pkg-config to compile the program. What the hell is that pkg-config? This blog post simply says.

What is the use of Pkgconfig:

You should all know that using a third-party library, you need to use the third party's header files and library files. When compiling and linking, we must specify the location of these header files and library files.

For a larger Third-party library, the number of header files and library files is more numerous. If we write each one manually, it would be quite troublesome. So, Pkg-config was born. Pkg-config can point to the location of these header files and library files and use them for the compiler. If your system is equipped with GTK, you can try the following command $pkg-config--cflags gtk+-2.0. You can see the path to the header file whose output is GTK.

This is how we normally use Pkg-config. $GCC main.c ' pkg-config--cflags--libs gtk+-2.0 '-o main

In the above compiler command, the role of ' Pkg-config--cflags--libs gtk+-2.0 ' As mentioned above, the GTK of the header file path and library files listed, let the compilation to get. --cflags and--libs specify header and library files, respectively.

Ps: ' Not quotes in the command, but the symbol on the left of the number 1.

In fact, Pkg-config has many options, like other commands, but we typically only use the--libs and--cflags options. More options are available for viewing here.

To Configure environment variables:

See here, you may want to try to use Pkg-config for your own library. Let's just say, how to write.

The first thing to be clear is that pkg-config is also just a command, so instead of installing a third-party library, Pkg-config will know where the header and library files of the Third-party library are located. The Pkg-config command is aware of this by querying the xxx.pc file. All we need to do is write a. pc file that belongs to your own library.

But Pkg-config is how to find the required. pc files. This requires the use of an environment variable Pkg_config_path. This environment variable identifies the path to the PC file, and the Pkg-config command reads the contents of the environment variable so that the PC file is known.

For Ubuntu systems, you can open the/ETC/BASH.BASHRC file with root permissions. Enter the following in the final section.


In this way, Pkg-config will go to the/usr/local/lib/pkgconfig directory, looking for the. pc file. If it is not an Ubuntu system, there is no/ETC/BASH.BASHRC file, you can refer to one of my blog posts, to find a suitable file, modify this environment variable. Enter the bash command to make the configuration effective.

Now Pkg-config can find our. pc files. But if there are multiple. pc files, then how can pkg-config correctly find the one I want? This requires us to specify when using the Pkg-config command. For example, $GCC main.c ' pkg-config--cflags--libs '-o main specifies the. pc file to be found is gtk+-2.0. For example, there is a third-party library OpenCV, and its corresponding PC file for opencv.pc, then we use, we should write ' Pkg-config--cflags--libs OpenCV '. In this way, Pkg-config will go to find opencv.pc files.

pc File writing specification:

Okay, now we're going to start writing our own. pc files. Just write 5 content: Name, Description, Version, Cflags, Libs.

For example, simple:

NAME:OPENCV
description:opencv pc file
version:2.4
cflags:-i/usr/local/include
libs:-l/usr/local /lib–lxxx–lxxx

Where name corresponds to the file name of this PC file. Of course, for the convenience of writing will also add a number of variables, such as prefix variable prefix. Here is a more complete content of the PC file

        , where Cflags and Libs are written using the compile options for-i-l-l these gcc. Principle can refer to one of my blog post.

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.