Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended way of using GLib2 with CMake

Tags:

cmake

glib

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?

like image 593
marmistrz Avatar asked Apr 26 '16 14:04

marmistrz


People also ask

What is CMake good for?

CMake handles the difficult aspects of building software such as cross-platform builds, system introspection, and user customized builds, in a simple manner that allows users to easily tailor builds for complex hardware and software systems.

Where is CMake used?

CMake is [...] software for managing the build process of software using a compiler-independent method. It is designed to support directory hierarchies and applications that depend on multiple libraries. It is used in conjunction with native build environments such as make, Apple's Xcode, and Microsoft Visual Studio.


2 Answers

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})
like image 42
bleater Avatar answered Oct 15 '22 10:10

bleater


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 glib-2.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)

like image 183
Ivan Avatar answered Oct 15 '22 09:10

Ivan