Linux c looks for ways to use cflags and libs for libraries

  • 2020-04-02 00:43:48
  • OfStack

Many times, you will need to add additional CFLAGS and LIBS when compiling executable programs with special libraries. Otherwise, you will be prompted to find the specified header file or "undefined reference to..." Error message.
Suppose the program test.c USES libxml's API, directly

$ gcc -Wall -o test test.c

An error message is prompted.  
perform

$ ls /usr/lib/pkgconfig/ | grep libxml
libxml-2.0.pc


$ cat /usr/lib/pkgconfig/libxml-2.0.pc 
prefix=/usr
exec_prefix=/usr
libdir=/usr/lib
includedir=/usr/include
modules=1
Name: libXML
Version: 2.6.26
Description: libXML library version2.
Requires:
Libs: -L${libdir} -lxml2  -lz  -lm 
Cflags: -I${includedir}/libxml2 

Where, Libs and Cflags are the required information

$ pkg-config libxml-2.0 --cflags --libs
-I/usr/include/libxml2  -lxml2 -lz -lm

Therefore, at compile time, only execution is required

$ gcc -Wall `pkg-config libxml-2.0 --cflags --libs` -o test test.c


Related articles: