从 GCC 转换为 CMake

Converting from GCC to CMake

什么是 CMake 等同于:

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

我对 $(pkg-config --libs --cflags mylib) 部分感兴趣,因为我已经有了可以编译的代码,我只需要添加 mylib 库。

查看 FindPkgConfig 模块
来源:https://cmake.org/cmake/help/latest/module/FindPkgConfig.html

CMakeLists.txt 示例:

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})