gcccmakepkg-configcflags

Converting from GCC to CMake


What's the CMake equivalent of:

gcc filename.c -o filename $(pkg-config --libs --cflags mylib)

I am interested on the $(pkg-config --libs --cflags mylib) part, since I already have code that compiles, and I just need to add the mylib library.


Solution

  • Take a look at the FindPkgConfig module
    src: https://cmake.org/cmake/help/latest/module/FindPkgConfig.html

    CMakeLists.txt example:

    include(FindPkgConfig)
    
    pkg_check_modules(MYLIB REQUIRED mylib)
    ...
    add_executable(filename filename.c)
    target_include_directories(filename PUBLIC ${MYLIB_INCLUDE_DIRS})
    target_compile_options(filename PUBLIC ${MYLIB_CFLAGS})
    target_link_libraries(filename ${MYLIB_LIBRARIES})