编译时pkg_config的用法

1 什么是pkg-config

pkg-config是一个linux下的命令,用于获得某一个库/模块的所有编译安装相关的信息。
例子:pkg-config opencv –libs –cflags
结果: -I/usr/include/opencv
/usr/lib/x86_64-linux-gnu/libopencv_calib3d.so /usr/lib/x86_64-linux-gnu/libopencv_contrib.so /usr/lib/x86_64-linux-gnu/libopencv_core.so /usr/lib/x86_64-linux-gnu/libopencv_features2d.so /usr/lib/x86_64-linux-gnu/libopencv_flann.so /usr/lib/x86_64-linux-gnu/libopencv_gpu.so /usr/lib/x86_64-linux-gnu/libopencv_highgui.so /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so /usr/lib/x86_64-linux-gnu/libopencv_legacy.so /usr/lib/x86_64-linux-gnu/libopencv_ml.so /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so /usr/lib/x86_64-linux-gnu/libopencv_ocl.so /usr/lib/x86_64-linux-gnu/libopencv_photo.so /usr/lib/x86_64-linux-gnu/libopencv_stitching.so /usr/lib/x86_64-linux-gnu/libopencv_superres.so /usr/lib/x86_64-linux-gnu/libopencv_ts.so /usr/lib/x86_64-linux-gnu/libopencv_video.so /usr/lib/x86_64-linux-gnu/libopencv_videostab.so

-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab

2 为什么要有pkg-config

从上面的例子,可以看出,pkg-config给出了opencv的头文件和库的所有信息!
这有什么好处?
所有用opencv的其他程序,在编译时,只需要写“pkg-config opencv –libs –cflags”,而不需要自己去找opencv的头文件在哪里,要链接的库在哪里!省时省力!
使用和不适用的对比:

g++ -g  test.cpp -o test  -I /usr/local/ImageMagick/include/ImageMagick-7  -L /usr/local/ImageMagick/lib/ -lMagick++-7.Q16HDRI 

g++ test.cpp  `pkg-config --cflags --libs Magick++-7.Q16HDRI`

特别在依赖第三库时:
如果Magick++-7.Q16HDRI依赖的库刚好被你的程序中使用和依赖,你将需要无休止的去查找依赖的库,而使用pkg-config,pkg-config将都会帮你搞定。

3 pkg-config的信息从哪里来?

很简单,有2种路径:
第一种:取系统的/usr/lib下的所有*.pc文件。
第二种:PKG_CONFIG_PATH环境变量所指向的路径下的所有*.pc文件。

这些pc文件什么时候有的?都是在你安装某个库/模块的时候,添加的。比如你往系统安装opencv时,就会在你的安装目录下,放一个/usr/local/opencv/lib64/pkgconfig/opencv.pc。 (我的安装目录/usr/local/opencv/)

[root@localhost tmp]# ls  /usr/local/opencv/lib64/pkgconfig/
opencv.pc

看看pc文件的具体内容

prefix=/usr/local/opencv
exec_prefix=${prefix}
libdir=${exec_prefix}/lib64
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.4.0
Libs: -L${exec_prefix}/lib64 -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -L${exec_prefix}/share/OpenCV/3rdparty/lib64 -littnotify -llibprotobuf -llibjpeg -llibwebp -llibtiff -llibjasper -lIlmImf -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lfreetype -L/lib64 -lpng -lz -lgstbase-0.10 -lgstreamer-0.10 -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lxml2 -lglib-2.0 -lgstvideo-0.10 -lgstapp-0.10 -lgstriff-0.10 -lgstpbutils-0.10 -ldc1394 -lavcodec -lavformat -lavutil -lswscale -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}

一目了然,就是存了所有opencv的头文件/库的路径信息。和第一步我们敲的”pkg-config opencv –libs –cflags”的结果,是一样一样的~~~

4 pkg-config都有哪些命令参数

所有参数,可以通过pkg-config –help来查看。

4.1 “pkg-config [NAME] –cflags”,查看头文件信息。(注意这里是2个“-”,不知道为啥我原文写2个“-”)

[root@localhost tmp]# pkg-config  opencv --cflags
-I/usr/local/opencv/include/opencv -I/usr/local/opencv/include 

4.2 pkg-config [NAME] –libs,查看库信息。

[root@localhost tmp]# pkg-config  opencv --libs
-L/usr/local/opencv/lib64 -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core  

4.3 pkg-config –list-all。查看pkg-config的所有模块信息。

[root@localhost tmp]# pkg-config  opencv --list-all
zlib                        zlib - zlib compression library
gio-unix-2.0                GIO unix specific APIs - unix specific headers for glib I/O library
xextproto                   XExtProto - XExt extension headers
libpostproc                 libpostproc - FFmpeg postprocessing library
cairo-xcb                   cairo-xcb - XCB surface backend for cairo graphics library
gstreamer-tag-0.10          GStreamer Tag Library - Tag base classes and helper functions
gio-2.0                     GIO - glib I/O library
gstreamer-pbutils-0.10      GStreamer Base Utils Library - General utility functions
xf86bigfontproto            XF86BigFontProto - XF86BigFont extension headers
libv4l1                     libv4l1 - v4l1 compatibility library
libv4l2                     libv4l2 - v4l2 device access library
libsystemd-daemon           systemd - systemd Daemon Utility Library - deprecated
xf86dgaproto                XF86DGAProto - XF86DGA extension headers
xcb-composite               XCB Composite - XCB Composite Extension
gstreamer-plugins-base-0.10 GStreamer Base Plugins Libraries - Streaming media framework, base plugins libraries
harfbuzz-icu                harfbuzz - HarfBuzz text shaping library ICU integration
libpng15                    libpng - Loads and saves PNG files
libsystemd-id128            systemd - systemd 128 Bit ID Utility Library - deprecated
xinerama                    Xinerama - The Xinerama Library
gstreamer-netbuffer-0.10    GStreamer Network Buffer Library - Network buffer for use in network sources/sinks
cairo-ps                    cairo-ps - PostScript surface backend for cairo graphics library
xcb-xevie                   XCB Xevie - XCB Xevie Extension
libswscale                  libswscale - FFmpeg image rescaling library
gmodule-2.0                 GModule - Dynamic module loader for GLib
libva-glx                   libva-glx - Userspace Video Acceleration (VA) glx interface
glib-2.0                    GLib - C Utility Library
ImageMagick                 ImageMagick - ImageMagick - convert, edit, and compose images (ABI Q16HDRI)
x264                        x264 - H.264 (MPEG4 AVC) encoder library
x265                        x265 - H.265/HEVC video encoder
randrproto                  RandrProto - Randr extension headers
gstreamer-riff-0.10         GStreamer RIFF Library - RIFF helper functions
libv4l2rds                  libv4l2rds - v4l2 RDS decode library
xau                         Xau - X authorization file management libary
gstreamer-check-0.10        GStreamer check unit testing - Unit testing helper library for GStreamer modules
dmxproto                    DMXProto - DMX extension headers
cairo-egl                   cairo-egl - EGL functions for cairo graphics library
xcb-damage                  XCB Damage - XCB Damage Extension
fontsproto                  FontsProto - Fonts extension headers
libyami                     libyami - Intel open source media infrastructure base on libva.
xineramaproto               XineramaProto - Xinerama extension headers
usbutils                    usbutils - USB device database
pango                       Pango - Internationalized text handling
MagickCore-7.Q16HDRI        MagickCore - MagickCore - C API for ImageMagick (ABI Q16HDRI)
xcb-xvmc                    XCB XvMC - XCB XvMC Extension
libavdevice                 libavdevice - FFmpeg device handling library
MagickWand                  MagickWand - MagickWand - C API for ImageMagick (ABI Q16HDRI)
libdrm                      libdrm - Userspace interface to kernel DRM services
gstreamer-sdp-0.10          GStreamer SDP Library - SDP helper functions
osmesa                      osmesa - Mesa Off-screen Rendering library
pciaccess                   pciaccess - Library providing generic access to the PCI bus and devices.
presentproto                PresentProto - Present extension headers
bash-completion             bash-completion - programmable completion for the bash shell
gl                          gl - Mesa OpenGL library
xf86miscproto               XF86MiscProto - XF86Misc extension headers
xproxymngproto              XProxyManagementProtocol - X Proxy Management Protocol headers
fontutil                    FontUtil - Font utilities dirs
xshmfence                   xshmfence - The X Shared Memory Fence Library
wayland-client              Wayland Client - Wayland client side library
glproto                     GLProto - GL extension headers
libpcrecpp                  libpcrecpp - PCRECPP - C++ wrapper for PCRE
gdk-pixbuf-2.0              GdkPixbuf - Image loading and scaling
pangoft2                    Pango FT2 and Pango Fc - Freetype 2.0 and fontconfig font support for Pango
libavutil                   libavutil - FFmpeg utility library
gdk-pixbuf-xlib-2.0         GdkPixbuf Xlib - GdkPixbuf rendering for Xlib
ice                         ICE - X Inter Client Exchange Library
gmodule-no-export-2.0       GModule - Dynamic module loader for GLib
libdrm_nouveau              libdrm_nouveau - Userspace interface to nouveau kernel DRM services
libavformat                 libavformat - FFmpeg container format library
xrender                     Xrender - X Render Library
libva                       libva - Userspace Video Acceleration (VA) core interface
wayland-server              Wayland Server - Server side implementation of the Wayland protocol
bigreqsproto                BigReqsProto - BigReqs extension headers
gnome-video-effects         gnome-video-effects - A collection of GStreamer effects to be used in different GNOME Modules
icu-io                      icu-io - International Components for Unicode: Stream and I/O Library
xproto                      Xproto - Xproto headers
liblzma                     liblzma - General purpose data compression library
xext                        Xext - Misc X Extension Library
xcb                         XCB - X-protocol C Binding
xcb-record                  XCB Record - XCB Record Extension
Magick++                    Magick++ - Magick++ - C++ API for ImageMagick (ABI Q16HDRI)
pthread-stubs               pthread stubs - Stubs missing from libc for standard pthread functions
xcb-xtest                   XCB XTEST - XCB XTEST Extension
videoproto                  VideoProto - Video extension headers
gtk+-unix-print-2.0         GTK+ - GTK+ Unix print support
gstreamer-audio-0.10        GStreamer Audio library - Audio helper functions and base classes
opencv                      OpenCV - Open Source Computer Vision Library
python-2.7                  Python - Python library
xcb-randr                   XCB RandR - XCB RandR Extension
gstreamer-controller-0.10   GStreamer controller - Dynamic parameter control for GStreamer elements
renderproto                 RenderProto - Render extension headers
systemd                     systemd - systemd System and Service Manager
xcb-present                 XCB Present - XCB Present Extension
gdk-2.0                     GDK - GTK+ Drawing Kit (x11 target)
gstreamer-dataprotocol-0.10 GStreamer data protocol library - Data protocol for plug-ins
gnome-icon-theme-symbolic   gnome-icon-theme-symbolic - A collection of symbolic icons used as the basis for GNOME themes
libavfilter                 libavfilter - FFmpeg audio/video filtering library
libsystemd                  systemd - systemd Library
gnome-keybindings           gnome-keybindings - Keybindings configuration for GNOME applications
glesv2                      glesv2 - Mesa OpenGL ES 2.0 library
libdvbv5                    libdvbv5 - DVBv5 utility library
xcursor                     Xcursor - X Cursor Library
libavcodec                  libavcodec - FFmpeg codec library
xi                          Xi - X Input Extension Library
xrandr                      Xrandr - X RandR Library
mysqlclient                 mysqlclient - MySQL client library
cairo-pdf                   cairo-pdf - PDF surface backend for cairo graphics library
egl                         egl - Mesa EGL library
xcb-sync                    XCB Sync - XCB Sync Extension
python                      Python - Python library
libraw1394                  libraw1394 - Interface library for the Linux IEEE1394 drivers.
xcomposite                  Xcomposite - X Composite Extension Library
expat                       expat - expat XML parser
xcb-xv                      XCB Xv - XCB Xv Extension
xcb-shm                     XCB Shm - XCB Shm Extension
x11-xcb                     X11 XCB - X Library XCB interface
libxml-2.0                  libXML - libXML library version2.
graphite2                   Graphite2 - Font rendering engine for Complex Scripts
xcb-shape                   XCB Shape - XCB Shape Extension
dracut                      dracut - dracut
xt                          Xt - X Toolkit Library
libudev                     libudev - Library to access udev device information
pangocairo                  Pango Cairo - Cairo rendering support for Pango
gstreamer-rtp-0.10          GStreamer RTP Library - RTP base classes and helper functions
fixesproto                  FixesProto - X Fixes extension headers
gmodule-export-2.0          GModule - Dynamic module loader for GLib
cairo-glx                   cairo-glx - GLX functions for cairo graphics library
gbm                         gbm - Mesa gbm library
xcb-xkb                     XCB XKB - XCB Keyboard Extension (EXPERIMENTAL)
dri3proto                   DRI3Proto - DRI3 extension headers
dri                         dri - Direct Rendering Infrastructure
x11                         X11 - X Library
cairo-xlib                  cairo-xlib - Xlib surface backend for cairo graphics library
xdamage                     Xdamage - X Damage  Library
libsystemd-journal          systemd - systemd Journal Utility Library - deprecated
xbitmaps                    X bitmaps - Bitmaps that are shared between X applications
damageproto                 DamageProto - Damage extension headers
xp                          Xp - X Print Library
kbproto                     KBProto - KB extension headers
xcb-xinput                  XCB XInput - XCB XInput Extension (EXPERIMENTAL)
xcb-dri2                    XCB DRI2 - XCB DRI2 Extension
xcb-dri3                    XCB DRI3 - XCB DRI3 Extension
pixman-1                    Pixman - The pixman library (version 1)
xcb-xinerama                XCB Xinerama - XCB Xinerama Extension
compositeproto              CompositeExt - Composite extension headers
cairo-xcb-shm               cairo-xcb-shm - XCB/SHM functions for cairo graphics library
gstreamer-0.10              GStreamer - Streaming media framework
xcb-xselinux                XCB SELinux - XCB SELinux Extension
libdrm_amdgpu               libdrm_amdgpu - Userspace interface to kernel DRM services for amdgpu
dri2proto                   DRI2Proto - DRI2 extension headers
xmuu                        Xmuu - Mini Xmu Library
libv4lconvert               libv4lconvert - v4l format conversion library
xxf86vm                     Xxf86vm - XFree86 Video Mode Extension Library
xf86driproto                XF86DRIProto - XF86DRI extension headers
icu-le                      icu-le - International Components for Unicode: Layout library
libpcre16                   libpcre16 - PCRE - Perl compatible regular expressions C library with 16 bit character support
inputproto                  InputProto - Input extension headers
resourceproto               ResourceProto - Resource extension headers
libdrm_radeon               libdrm_radeon - Userspace interface to kernel DRM services for radeon
gobject-2.0                 GObject - GLib Type, Object, Parameter and Signal Library
Magick++-7.Q16HDRI          Magick++ - Magick++ - C++ API for ImageMagick (ABI Q16HDRI)
ImageMagick-7.Q16HDRI       ImageMagick - ImageMagick - convert, edit, and compose images (ABI Q16HDRI)
libpng                      libpng - Loads and saves PNG files
xcb-dpms                    XCB DPMS - XCB DPMS Extension
cairo-fc                    cairo-fc - Fontconfig font backend for cairo graphics library
fontconfig                  Fontconfig - Font configuration and customization library
libva-egl                   libva-egl - Userspace Video Acceleration (VA) egl interface
MagickWand-7.Q16HDRI        MagickWand - MagickWand - C API for ImageMagick (ABI Q16HDRI)
cairo                       cairo - Multi-platform 2D graphics library
python2                     Python - Python library
icu-lx                      icu-lx - International Components for Unicode: Paragraph Layout library
gthread-2.0                 GThread - Thread support for GLib
libpcreposix                libpcreposix - PCREPosix - Posix compatible interface to libpcre
wayland-cursor              Wayland Cursor - Wayland cursor helper library
xmu                         Xmu - Xmu Library
gstreamer-video-0.10        GStreamer Video Library - Video base classes and helper functions
xatracker                   xatracker - Xorg Gallium3D acceleration library
gstreamer-interfaces-0.10   GStreamer Interfaces Library - Interfaces for GStreamer elements
pangoxft                    Pango Xft - Xft font support for Pango
cairo-png                   cairo-png - PNG functions for cairo graphics library
cairo-ft                    cairo-ft - FreeType font backend for cairo graphics library
xcb-res                     XCB Res - XCB X-Resource Extension
wayland-scanner             Wayland Scanner - Wayland scanner
xft                         Xft - X FreeType library
gstreamer-net-0.10          GStreamer networking library - Network-enabled GStreamer plug-ins and clocking
cairo-xlib-xrender          cairo-xlib-xrender - Xlib Xrender surface backend for cairo graphics library
libdrm_intel                libdrm_intel - Userspace interface to intel kernel DRM services
atk                         Atk - Accessibility Toolkit
xf86vidmodeproto            XF86VidModeProto - XF86VidMode extension headers
gstreamer-floatcast-0.10    GStreamer Floatcast Library - Platform independent floating point macros
libva-x11                   libva-x11 - Userspace Video Acceleration (VA) x11 interface
gstreamer-cdda-0.10         GStreamer CDDA Library - CDDA base classes
gtk+-x11-2.0                GTK+ - GTK+ Graphical UI Library (x11 target)
libva-drm                   libva-drm - Userspace Video Acceleration (VA) drm interface
glw                         glw - Mesa OpenGL widget library
icu-uc                      icu-uc - International Components for Unicode: Common and Data libraries
cairo-svg                   cairo-svg - SVG surface backend for cairo graphics library
gstreamer-app-0.10          GStreamer Application Library - Helper functions and base classes for application integration
printproto                  PrintProto - Print extension headers
wayland-egl                 wayland-egl - Mesa wayland-egl library
libdc1394-2                 libdc1394 - 1394-based DC Control Library
sm                          SM - X Session Management Library
glu                         glu - Mesa OpenGL Utility library
check                       Check - A unit test framework for C
shared-mime-info            shared-mime-info - Freedesktop common MIME database
harfbuzz                    harfbuzz - HarfBuzz text shaping library
cairo-gl                    cairo-gl - OpenGL surface backend for cairo graphics library
icu-i18n                    icu-i18n - International Components for Unicode: Internationalization library
xcb-render                  XCB Render - XCB Render Extension
recordproto                 RecordProto - Record extension headers
gstreamer-base-0.10         GStreamer base classes - Base classes for GStreamer elements
libpcre                     libpcre - PCRE - Perl compatible regular expressions C library with 8 bit character support
gdk-x11-2.0                 GDK - GTK+ Drawing Kit (x11 target)
cairo-script                cairo-script - script surface backend for cairo graphics library
libpcre32                   libpcre32 - PCRE - Perl compatible regular expressions C library with 32 bit character support
gstreamer-rtsp-0.10         GStreamer RTSP Library - RTSP base classes and helper functions
xcmiscproto                 XCMiscProto - XCMisc extension headers
libkms                      libkms - Library that abstract aways the different mm interface for kernel drivers
libswresample               libswresample - FFmpeg audio resampling library
cairo-tee                   cairo-tee - tee surface backend for cairo graphics library
freetype2                   FreeType 2 - A free, high-quality, and portable font engine.
xcb-xf86dri                 XCB XFree86-DRI - XCB XFree86-DRI Extension
MagickCore                  MagickCore - MagickCore - C API for ImageMagick (ABI Q16HDRI)
xfixes                      Xfixes - X Fixes  Library
gstreamer-fft-0.10          GStreamer FFT Library - FFT implementation
xcb-xfixes                  XCB XFixes - XCB XFixes Extension
libva-tpi                   libva-tpi - Userspace Video Acceleration (VA) 3rd party interface
xcb-screensaver             XCB Screensaver - XCB Screensaver Extension
gtk+-2.0                    GTK+ - GTK+ Graphical UI Library (x11 target)
gail                        Gail - GNOME Accessibility Implementation Library
evieproto                   EvIEExt - EvIE extension headers
libva-wayland               libva-wayland - Userspace Video Acceleration (VA) wayland interface
xcb-glx                     XCB GLX - XCB GLX Extension
libsystemd-login            systemd - systemd Login Utility Library - deprecated
scrnsaverproto              ScrnSaverProto - ScrnSaver extension headers
udev                        udev - udev

你可能感兴趣的:(编译时pkg_config的用法)