0

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.

Kevin
  • 14,269
  • 7
  • 44
  • 64
Michel Feinstein
  • 11,892
  • 14
  • 81
  • 164

1 Answers1

1

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})
Mizux
  • 5,714
  • 3
  • 21
  • 42
  • Does the order of the statements matter? `add_executable` has to be in that position, or can it come first or last? – Michel Feinstein Sep 16 '19 at 21:38
  • add_executable(foo ...) (ed or add_library(foo ...)) will generate (i.e. declare) the CMake target *foo* so it must comes before any other target_*() functions after you can put the target_* methods in any order... – Mizux Sep 18 '19 at 08:30