pkg-config and CMake
If the library that you want to use is not coming with Find<package>. cmake you can use pkg-config files which are stored in “.pc”. The environmental variable PKG_CONFIG_PATH is the place that should point to the “.pc” files.
Type in the shell:
1 2 3 |
PKG_CONFIG_PATH=<path-to-directory-containing-the-.pc-file>:${PKG_CONFIG_PATH} export PKG_CONFIG_PATH printenv PKG_CONFIG_PATH |
To get the list of all available package configs, you can type:
1 |
pkg-config --list-all |
How to set an environmental variable in CMake file:
1 |
SET(ENV{PKG_CONFIG_PATH} "<path-to-directory-containing-the-.pc-file>:" $ENV{PKG_CONFIG_PATH}) |
Now how to print environmental variables in CMake file:
1 |
MESSAGE("HOME: "$ENV{HOME}) |
Flann
To get the libs and version, type in shell :
1 2 3 |
pkg-config --cflags <name-from-above-list> pkg-config --libs flann pkg-config --modversion flann |
After setting environmental variable PKG_CONFIG_PATH pointing to your .pc file, you can call:
1 2 3 4 5 6 7 8 9 10 11 12 |
pkg_search_module(FLANN REQUIRED flann) if(${FLANN_FOUND}) MESSAGE("FLANN_FOUND:" ${FLANN_FOUND}) MESSAGE("FLANN_VERSION:" ${FLANN_VERSION}) MESSAGE("FLANN_LIBRARIES:" ${FLANN_LIBRARIES}) MESSAGE("FLANN_INCLUDE_DIRS:" ${FLANN_INCLUDEDIR}) MESSAGE("FLANN_LIBRARY_DIRS:" ${FLANN_LIBDIR}) INCLUDE_DIRECTORIES(${FLANN_INCLUDEDIR}) LINK_DIRECTORIES(${FLANN_LIBDIR}) ADD_EXECUTABLE(main src/main.cpp) TARGET_LINK_LIBRARIES(main ${FLANN_LIBRARIES}) endif() |
gRPC
gRPC as another example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
find_package(PkgConfig REQUIRED) #for c pkg_search_module(GRPC REQUIRED grpc) #for c++ pkg_search_module(GRPCPP REQUIRED grpc++) if(${GRPCPP_FOUND}) MESSAGE("GRPCPP_FOUND:" ${GRPCPP_FOUND}) MESSAGE("GRPCPP_VERSION:" ${GRPCPP_VERSION}) MESSAGE("GRPCPP_LIBRARIES:" ${GRPCPP_LIBRARIES}) MESSAGE("GRPCPP_INCLUDE_DIRS:" ${GRPCPP_INCLUDEDIR}) MESSAGE("GRPCPP_LIBRARY_DIRS:" ${GRPCPP_LIBDIR}) INCLUDE_DIRECTORIES(${GRPCPP_INCLUDEDIR}) LINK_DIRECTORIES(${GRPCPP_LIBDIR}) ADD_EXECUTABLE(main src/main.cpp) TARGET_LINK_LIBRARIES(main ${GRPCPP_LIBRARIES}) endif() |
TinyXML2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
SET(ENV{PKG_CONFIG_PATH} "$ENV{HOME}/usr/lib/pkgconfig:" $ENV{PKG_CONFIG_PATH}) MESSAGE("PKG_CONFIG_PATH:" $ENV{PKG_CONFIG_PATH}) find_package(PkgConfig) pkg_check_modules(TINYXML2 tinyxml2) if(${TINYXML2_FOUND}) MESSAGE("TINYXML2_FOUND:" ${TINYXML2_FOUND}) MESSAGE("TINYXML2_VERSION:" ${TINYXML2_VERSION}) MESSAGE("TINYXML2_LIBRARIES:" ${TINYXML2_LIBRARIES}) MESSAGE("TINYXML2_INCLUDE_DIRS:" ${TINYXML2_INCLUDE_DIRS}) MESSAGE("TINYXML2_LIBRARY_DIRS:" ${TINYXML2_LIBRARY_DIRS}) INCLUDE_DIRECTORIES(${TINYXML2_INCLUDE_DIRS}) LINK_DIRECTORIES(${TINYXML2_LIBRARY_DIRS}) ADD_EXECUTABLE(tinyxml2_demo src/third_party_tools/xml/tinyxml2/tinyxml2_demo.cpp) TARGET_LINK_LIBRARIES(tinyxml2_demo ${TINYXML2_LIBRARIES}) endif() |
Google Benchmark
1 2 3 4 5 6 7 8 9 10 11 12 |
pkg_check_modules(BENCHMARK benchmark) if(${BENCHMARK_FOUND}) MESSAGE("BENCHMARK_FOUND:" ${BENCHMARK_FOUND}) MESSAGE("BENCHMARK_VERSION:" ${BENCHMARK_VERSION}) MESSAGE("BENCHMARK_LIBRARIES:" ${BENCHMARK_LIBRARIES}) MESSAGE("BENCHMARK_INCLUDE_DIRS:" ${BENCHMARK_INCLUDE_DIRS}) MESSAGE("BENCHMARK_LIBRARY_DIRS:" ${BENCHMARK_LIBRARY_DIRS}) INCLUDE_DIRECTORIES(${TINYXML2_INCLUDE_DIRS}) LINK_DIRECTORIES(${TINYXML2_LIBRARY_DIRS}) ADD_EXECUTABLE(benchmark_demo src/third_party_tools/benchmark/benchmark_demo.cpp) TARGET_LINK_LIBRARIES(benchmark_demo ${BENCHMARK_LIBRARIES} pthread) endif() |