Use of PKG-config

Source: Internet
Author: User
Tags gtk

I. Introduction to PKG-config

1. For a brief introduction to the PKG-config tool, see the following link:

Understand the PKG-config Tool

2. PKG-config software Official Website:Http://www.freedesktop.org/wiki/Software/pkg-config/

 Ii. Recently I have been reading some examples of glib, libraries, and GTK. During program compilation, I often cannot find the header file and library file path. In addition, PKG-config is used for RPM packaging and compilation using cmake.

PKG-config can be installed Based on the software. PC configuration file path to find the corresponding header file path and library file path, such as my ubuntu12.04 default installation after glib-2.0 in/usr/lib/i386-linux-gnu/pkgconfig/

There is a glib-2.0.pc under the path, its content is as follows:

prefix=/usrexec_prefix=${prefix}libdir=${prefix}/lib/i386-linux-gnuincludedir=${prefix}/includeglib_genmarshal=glib-genmarshalgobject_query=gobject-queryglib_mkenums=glib-mkenumsName: GLibDescription: C Utility LibraryVersion: 2.32.3Requires.private: libpcreLibs: -L${libdir} -lglib-2.0 Libs.private: -pthread -lrt  -lpcre    Cflags: -I${includedir}/glib-2.0 -I${libdir}/glib-2.0/include 

Based on the description of the glib-2.0.pc file, we can see that the path to glib-2.0 after using sudo apt-Get install glib-2.0 is as follows:

Executable File Installation path:/usr

. H header file installation path:/usr/include/glib-2.0 and/usr/lib/i386-linux-gnu/glib-2.0/include

. So Library File Installation path:/usr/lib/i386-linux-gnu


3. After installing glib-2.0 and GTK +-2.0 in ubuntu12.04, let's take a look at two examples.

1. A simple program Hello. C using the glib library, the Code is as follows:

//hello.c#include <glib.h>int main(int argc, char *argv[]){    g_print("welcome to glib programming world!\n");    return 0;}


Open the terminal and enter gcc-O hello. c 'pkg-config -- cflags -- libs glib-100' (note: this is not a single quotation mark, but the numeric key '1' on the left side of the keyboard and ['] on the top of the tab key), do not make a mistake !)

CCF @ ccf-F81Se :~ /Workplace/glib library learning $ gcc-O hello. c 'pkg-config -- cflags -- libs glib-2.0 'CCF @ ccf-F81Se :~ /Workplace/glib library learning $ lsdbus-glib .ppt glib-2.34.3-docs glib library introduction ~ Gmainloop. C ~ Loop thread_pools_glib.pdfevent_loop.html the installation and compilation of the glib library of the glib-2.34.3.tar.xz? Gmain_demo.c ~ Hello loop. c understand PKG-config tool glib-2.34.3 glib-notes glib library introduction gmainloop. c hello. c makefile understand PKG-config tool ~ CCF @ ccf-F81Se :~ /Workplace/glib library learning $./Hello welcome to glib programming world! CCF @ ccf-F81Se :~ /Workplace/glib library learning $


2. A simple GTK Program

/** File name: gtkbase. C * // # include <gtk-2.0/GTK. h> # include <GTK/GTK. h> // # include <GTK. h> // if this path is used, the compilation will fail and the header file cannot be found! Int main (INT argc, char * argv []) {gtkwidget * WND; gtk_init (& argc, & argv); WND = gtk_window_new (gtk_window_toplevel); gtk_widget_show (WND ); gtk_main (); Return 0 ;}

Compile the following command on the terminal: GCC gtkbase. C-o gtkbase 'pkg-config -- cflags -- libs GTK +-2.0 gthread-2.0'

CCF @ ccf-F81Se :~ /Workplace/GTK $ lsgtk2.0 compilation problem gtkbase. c makefile threadloop. cgtk2.0 compilation problem ~ Gtkbase. C ~ Threadloopccf @ ccf-F81Se :~ /Workplace/GTK $ GCC gtkbase. C-o gtkbase 'pkg-config -- cflags -- libs GTK +-2.0 gthread-2.0 'CCF @ ccf-F81Se :~ /Workplace/GTK $./gtkbase

For the sake of simplicity, you can write a simple MAKEFILE file. In this way, you only need to modify a few parts each time you compile the file. In the terminal, type make or make clean.

#makefile for gtkbaseAPP_NAME=gtkbaseCC=gccRM=-rm -fLIBS=`pkg-config --cflags --libs gtk+-2.0 gthread-2.0`$(APP_NAME):$(APP_NAME).c    gcc $< -o $@ $(LIBS).PHNOY:cleanclean:    $(RM) *.o $(APP_NAME)

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.