19

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?

marmistrz
  • 5,974
  • 10
  • 42
  • 94
  • if glib is installed (which it usually is) then gcc will use it unless instructed not to (with a compiler flag). – Uri Brecher Apr 26 '16 at 14:45
  • But then this example doesn't compile: http://paste.ubuntu.com/16064301/ because of `glib.h` not found – marmistrz Apr 26 '16 at 15:13
  • Look at this thread (http://stackoverflow.com/questions/10383830/including-glib-h-in-a-cmake-project) – Uri Brecher Apr 26 '16 at 15:19
  • But it's not clear to me whether I should use the `pkg-config` approach taken by the OP, or whether I should copy `FindGLib.cmake` to my root directory. And is this one version aware? – marmistrz Apr 26 '16 at 15:24
  • 1
    When using cmake I would skip pkg-config and write my own package module. Glib seems so basic that I expected a module to come preinstalled with cmake – Uri Brecher Apr 26 '16 at 15:28
  • Well, when using the linked module, cmake complains it can't find some libfindmacros. Seems it's in the repo, but this ceases to be an elegant solution for a not so big project – marmistrz Apr 26 '16 at 15:39
  • @UriBrecher, you’re confusing GLib and glibc. The compiler will use glibc (or a libc implementation) by default. GLib is a non-system utility library whose include paths and linker flags need to be explicitly added to the compiler command line, typically using pkg-config. – Philip Withnall Sep 21 '18 at 10:44

6 Answers6

14

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)

parsley72
  • 8,449
  • 8
  • 65
  • 98
Ivan
  • 1,552
  • 1
  • 16
  • 25
  • Correction: `IMPORTED_TARGET` appeared in CMake 3.6, as you can see by noticing that 3.5 docs still didn't mention it (and it actually doesn't work, tested with CMake 3.5.2): https://cmake.org/cmake/help/v3.5/module/FindPkgConfig.html – j1elo Jul 06 '19 at 14:18
  • Right, CMake 3.6 (released in July 2016) – Ivan Jul 24 '19 at 05:32
11

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})
bleater
  • 5,098
  • 50
  • 48
  • Why do you use the `INTERFACE` keyword? This does not link glib to the target according to the [docs](https://cmake.org/cmake/help/latest/command/target_link_libraries.html#libraries-for-a-target-and-or-its-dependents). – Lukas Juhrich Mar 16 '19 at 16:53
  • 2
    The OP stated he was linking an application, which would typically be a dynamically-linked target. Since GLib is a system library, using INTERFACE reather than PUBLIC may avoid linking at build time to a possibly static version of Llib, instead writing it into the dynamic dependencies and deferring it to the dynamic linker. Some of this behaviour depends on the system configuration at the time of build though. – bleater Mar 18 '19 at 03:31
  • Is there a way to link to the static library as my runtime environment doesn't support it – s f Apr 06 '22 at 15:08
  • @sf Cmake docs say: `The following variables may be set upon return. Two sets of values exist: One for the common case ( = ) and another for the information pkg-config provides when called with the --static option ( = _STATIC).` So you could try `GLIB_LDFLAGS_STATIC`. – bleater Apr 07 '22 at 03:25
  • I tried target_link_libraries(${TARGET} ${GLIB_STATIC_LDFLAGS}) but it still links to libglib-2.0.so.0; then I tried `gcc ../src/main.c -o glibtest \`pkg-config --libs --static glib-2.0\` \`pkg-config --cflags glib-2.0\`, same result; I'm sure /usr/lib64/libglib-2.0.a exists on my device – s f Apr 07 '22 at 14:14
  • The docs refer to GLIB_LDFLAGS_STATIC, not GLIB_STATIC_LDFLAGS. – bleater Apr 09 '22 at 09:42
5

Give a look at my answer on using CMake with GTK

It's pretty much the same with GLib.

Community
  • 1
  • 1
liberforce
  • 11,189
  • 37
  • 48
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

ebassi
  • 8,648
  • 27
  • 29
4

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})
Barmaley
  • 1,232
  • 20
  • 27
1

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.

nemequ
  • 16,623
  • 1
  • 43
  • 62