zoukankan      html  css  js  c++  java
  • 库编译依赖处理小利器:pkg-config

    一、简介

    1. 关于它的定义(From 官网):

    pkg-config is a helper tool used when compiling applications and libraries. It helps you insert the correct compiler options on the command line so an application can use gcc -o test test.c `pkg-config --libs --cflags glib-2.0` for instance, rather than hard-coding values on where to find glib (or other libraries). It is language-agnostic, so it can be used for defining the location of documentation tools, for instance.

    简言之就是,利用了 pkg-config 后,开发人员在编译、链接三方库的时候,不必再写 hard-code 来声明需依赖的头文件或库的路径,通过该工具即可以实现依赖的自动捆绑,以提高工作效率。

    二、使用

    1.查看当前系统中已经关联到pkg-config的库列表:pkg-config --list-all

    glib-2.0                       glib-2.0 - The glib library for arm64.
    libnl-3.0                      libnl - Convenience library for netlink sockets
    libical                        libical - An implementation of basic iCAL protocols
    gio-2.0                        GIO - glib I/O library
    libffi                         libffi - Library supporting Foreign Function Interfaces
    readline                       readline - The readline library for arm64.
    expat                          expat - expat XML parser
    dbus-1                         dbus - Free desktop message bus
    zlib                           zlib - The zlib library for arm64. 

    2.输出指定库的详细路径信息,以 libffi 库为例:pkg-config libffi -libs -cflags

    -I/home/xxx/tools/libffi-3.3/_install/include -L/home/xxx/tools/libffi-3.3/_install/lib/../lib64 -lffi

    通过该命令即可实现三方库的自动查找,在许多库源码的 confiure 中就是应用的该方法。

    三、原理

    是不是说一个库编译出来后会自动关联到 pkg-config?其实不然,它还没有那么的智能。

    pkg-config 会到默认的路径中去搜索配置文件,如果某的库的配置文件添加到 pkg-config 覆盖到的路径中,那么该库就会被加入到 pkg-config 的列表当中。

    配置文件的名称为:*.pc

    通常的配置内容为:

    prefix=/home/xxx/tools/libffi-3.3/_install
    exec_prefix=${prefix}
    libdir=${exec_prefix}/lib
    toolexeclibdir=${libdir}/../lib64
    includedir=${prefix}/include
    
    Name: libffi
    Description: Library supporting Foreign Function Interfaces
    Version: 3.3
    Libs: -L${toolexeclibdir} -lffi
    Cflags: -I${includedir}

    在平时的开发中,我们也可以应用这种配置方法来加入自定义的库到 pkg-config 的列表中。

    四、总结

    到目前已经基本理解了 pkg-config 的使用方法以及原理,在开发中应用起来应该没有什么问题。

    pkg-config 没有按平台管理库的功能,那是否存在这样一个问题,在交叉编译不同平台的时候会不会关联到本平台的库呢?

    我的理解是会的!但这一定会导致错误,是否有解决的办法呢?有的!

    通过 PKG_CONFIG_PATH 环境变量指定交叉编译平台所涉及的配置文件目录,pkg-config 在搜索时会优先搜寻该路径。

     

    感谢花费宝贵的时间浏览, 转载请注明出处。 本人将在[资源共享]分类下陆续加入学习过程中一些比较重要且有用处的资料、源码,大家可前往下载,一起进步。 感谢支持!
  • 相关阅读:
    iOS中Zbar二维码扫描的使用
    SOJ 1135. 飞跃原野
    SOJ 1048.Inverso
    SOJ 1219. 新红黑树
    SOJ 1171. The Game of Efil
    SOJ 1180. Pasting Strings
    1215. 脱离地牢
    1317. Sudoku
    SOJ 1119. Factstone Benchmark
    soj 1099. Packing Passengers
  • 原文地址:https://www.cnblogs.com/GyForever1004/p/14539743.html
Copyright © 2011-2022 走看看