What is the recommended way of using GLib2 with CMake

瘦欲@ 提交于 2020-08-18 01:42:33

问题


Id like to use GLib in my C application which uses CMake as the build system.

Now, I'm somehow confused how I should enable GLib in my CMakeLists.txt. Basically, you add libraries in cmake using the find_package command, so I tried, according to this bugreport

find_package(GLib2)

But nothing is found. In the GLib documentation it is suggested to use pkg-config, on the other hand.

What is the recommended way of enabling glib in a cmake-based project?


回答1:


Give a look at my answer on using CMake with GTK

It's pretty much the same with GLib.




回答2:


Since CMake 3.6 (released in July 2016), pkg_check_modules supports IMPORTED_TARGET argument, reducing the dependency configuration to a single target_link_libraries statement, which will take care of all required compiler and linker options:

find_package(PkgConfig REQUIRED)
pkg_check_modules(deps REQUIRED IMPORTED_TARGET gtk+-3.0)
target_link_libraries(target PkgConfig::deps)

(above I used the name deps because one can list multiple dependencies with a single pkg_check_modules statement)




回答3:


In your CMakeLists.txt:

find_package(PkgConfig REQUIRED)
pkg_search_module(GLIB REQUIRED glib-2.0)
target_include_directories(mytarget PRIVATE ${GLIB_INCLUDE_DIRS})
target_link_libraries(mytarget INTERFACE ${GLIB_LDFLAGS})



回答4:


GLib (and various other C libraries using autotools) provide a pkg-config file for declaring:

  • compiler flags
  • linker flags
  • build-time variables
  • dependencies

The appropriate way to discover where these libraries are with CMake is to use the FindPkgConfig CMake module:

https://cmake.org/cmake/help/v3.0/module/FindPkgConfig.html




回答5:


I've been working on some CMake modules for GNOME (including one for GLib) which you might want to try. Basically, just find_package(GLib), then you can use the glib-2.0 imported target to link to it.




回答6:


yet another version, combination of multiple answers and what actually worked for me (on Linux)!

cmake_minimum_required(VERSION 2.6.4)
project(my_proj)

find_package(PkgConfig REQUIRED)
pkg_search_module(GLIB REQUIRED glib-2.0)

include_directories(${GLIB_INCLUDE_DIRS})
link_directories(${GLIB_LIBRARY_DIRS})

add_executable(my_proj main.c)

add_definitions(${GLIB_CFLAGS_OTHER})
target_link_libraries(my_proj ${GLIB_LIBRARIES})


来源:https://stackoverflow.com/questions/36868143/what-is-the-recommended-way-of-using-glib2-with-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!