合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[TOC] ## pkg-config pkg-config 是一个帮助在编译和链接程序时管理库路径和编译器标志的工具。它主要用于自动检索库的路径和编译选项,而不需要手动指定。 pkg-config 默认系统默认位置 ``` /usr/lib/pkgconfig /usr/share/pkgconfig /usr/local/lib/pkgconfig /usr/local/share/pkgconfig ``` ## 语法 ``` --version output version of pkg-config --modversion output version for package --atleast-pkgconfig-version=VERSION require given version of pkg-config --libs output all linker flags --static output linker flags for static linking --short-errors print short errors --libs-only-l output -l flags --libs-only-other output other libs (e.g. -pthread) --libs-only-L output -L flags --cflags output all pre-processor and compiler flags --cflags-only-I output -I flags --cflags-only-other output cflags not covered by the cflags-only-I option --variable=NAME get the value of variable named NAME --define-variable=NAME=VALUE set variable NAME to VALUE --exists return 0 if the module(s) exist --print-variables output list of variables defined by the module --uninstalled return 0 if the uninstalled version of one or more module(s) or their dependencies will be used --atleast-version=VERSION return 0 if the module is at least version VERSION --exact-version=VERSION return 0 if the module is at exactly version VERSION --max-version=VERSION return 0 if the module is at no newer than version VERSION --list-all list all known packages --debug show verbose debug information --print-errors show verbose information about missing or conflicting packages,default if --cflags or --libs given on the command line --silence-errors be silent about errors (default unless --cflags or --libsgiven on the command line) --errors-to-stdout print errors from --print-errors to stdout not stderr --print-provides print which packages the package provides --print-requires print which packages the package requires --print-requires-private print which packages the package requires for static linking ``` ### --modversion 查看版本 ``` > pkg-config --modversion json 0.11 ``` ### --variable 查找pkg-config 所在位置 ``` pkg-config --variable pc_path pkg-config ``` ### --list-all 查看系统中的所有库 ``` > pkg-config --list-all libffi libffi - Library supporting Foreign Function Interfaces libselinux libselinux - SELinux utility library tic tic - ncurses 5.9 add-on library libmongoc-ssl-1.0 libmongoc-1.0 - SSL support for the libmongoc-1.0 library. ncurses ncurses - ncurses 5.9 library com_err com_err - Common error description library json json-c - JSON implementation in C, compat shim. Use json-c instead. ... ``` ### --libs/--cflags ``` // 查看库所在位置 > pkg-config --libs json -L/lib64 -ljson-c // 查看头文件所在位置 > pkg-config --cflags json -I/include -I/include/json-c ``` > 依赖编译时候同时使用 `--libs --cflags` 即可自动输出库所在位置和头文件所在位置 ## 示例 ### 引用 linux 中的 json 库 vim json.cpp ``` #include <json.h> #include <stdio.h> int main() { struct json_object *jobj; char *str = "{ \"hello\": \"word\" }"; printf("str:\n---\n%s\n---\n\n", str); jobj = json_tokener_parse(str); printf("jobj from str:\n---\n%s\n---\n", json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY)); return 0; } ``` 编译 ``` > g++ -o json json.cpp `pkg-config --libs --cflags json > ./json str: --- { "hello": "word" } --- jobj from str: --- { "hello": "word" } --- ``` ### 创建 pkg-config 项目 假设项目位置位于 /root 下 ``` mymath --mymath.pc --include/ --mymath.h --src/ --mymath.cpp --lib/ --libmymath.a # 库文件 ``` mymath.pc ``` # mymath.pc librootdir=/root/mymath Name: mymath Description: a simple add function Version: 1.0 Cflags: -I${librootdir}/include Libs: -L${librootdir}/lib -lmymath ``` include/mymath.h ``` namespace mymath { int add(int a, int b); }; ``` src/mymath.cpp ``` #include "mymath.h" #include <iostream> namespace mymath { int add(int a, int b) { std::cout << "Add " << a << " and " << b << " is " << a + b << std::endl; return a + b; } }; ``` 编译 ``` > cd mymath > g++ -o mymath.o -c src/mymath.cpp -Iinclude > ar rcs lib/libmymath.a mymath.o ``` 创建一个引用 次项目的项目 test.cpp ``` #include "mymath.h" int main(int argc, char** argv) { mymath::add(1, 2); return 0; } ``` 编译 ``` // 通过 PKG_CONFIG_PATH 变量指定 or 也可以放到系统默认位置中 // cp /root/mymath/mymath.pc /usr/share/pkgconfig/ > export PKG_CONFIG_PATH=/root/mymath/ > g++ -o test test.cpp `pkg-config --cflags --libs mymath` > ./test Add 1 and 2 is 3 ```