What is the recommended way of using GLib2 with CMake
Asked Answered
R

6

23

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?

Requite answered 26/4, 2016 at 14:38 Comment(7)
if glib is installed (which it usually is) then gcc will use it unless instructed not to (with a compiler flag).Histogen
But then this example doesn't compile: paste.ubuntu.com/16064301 because of glib.h not foundRequite
Look at this thread (#10384330)Histogen
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?Requite
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 cmakeHistogen
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 projectRequite
@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.Ouellette
R
6

Give a look at my answer on using CMake with GTK

It's pretty much the same with GLib.

Rigmarole answered 20/5, 2016 at 12:41 Comment(0)
L
16

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)

Lore answered 21/9, 2018 at 9:59 Comment(2)
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): cmake.org/cmake/help/v3.5/module/FindPkgConfig.htmlNancinancie
Right, CMake 3.6 (released in July 2016)Lore
C
13

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})
Circumnutate answered 21/2, 2018 at 2:13 Comment(6)
Why do you use the INTERFACE keyword? This does not link glib to the target according to the docs.Arciniega
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.Circumnutate
Is there a way to link to the static library as my runtime environment doesn't support itGuardian
@sf Cmake docs say: The following variables may be set upon return. Two sets of values exist: One for the common case (<XXX> = <prefix>) and another for the information pkg-config provides when called with the --static option (<XXX> = <prefix>_STATIC). So you could try GLIB_LDFLAGS_STATIC.Circumnutate
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 deviceGuardian
The docs refer to GLIB_LDFLAGS_STATIC, not GLIB_STATIC_LDFLAGS.Circumnutate
R
6

Give a look at my answer on using CMake with GTK

It's pretty much the same with GLib.

Rigmarole answered 20/5, 2016 at 12:41 Comment(0)
S
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

Swami answered 26/4, 2016 at 15:54 Comment(0)
G
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})
Goode answered 30/1, 2019 at 23:45 Comment(0)
J
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.

Jeramey answered 21/5, 2016 at 19:29 Comment(2)
Thank you for the MIT license. Only other one I could find was GPL2.0.Wellordered
@Wellordered Do you have a link to that other one?Devoid

© 2022 - 2024 — McMap. All rights reserved.