# A Gtk4 CMakeLists.txt example that includes gmodule-export-2.0

**URL:** https://discourse.cmake.org/t/a-gtk4-cmakelists-txt-example-that-includes-gmodule-export-2-0/13052
**Category:** Code
**Tags:** os:linux
**Created:** [November 25, 2024, 4:41am UTC](https://discourse.cmake.org/t/a-gtk4-cmakelists-txt-example-that-includes-gmodule-export-2-0/13052 "2024-11-25T04:41:48Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![ferdnyc](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ferdnyc/32/267_2.png) [@ferdnyc](https://discourse.cmake.org/u/ferdnyc)
#### Post date: [November 25, 2024, 11:58am UTC](https://discourse.cmake.org/t/a-gtk4-cmakelists-txt-example-that-includes-gmodule-export-2-0/13052/2 "2024-11-25T11:58:02Z")

</div>

> [@oDinZu](#):
>
> I also tried doing `set(CMAKE_EXECUTABLE_ENABLE_EXPORTS TRUE)`, but am a bit baffled in the differences of setting that command and or including the C\_Flags.

I don’t believe that’s related to the sort of exports that gmodule is looking for, and regardless it’s unnecessary because…

> [@oDinZu](#):
>
> “applications should instead be compiled with the -Wl,–export-dynamic argument inside their compiler flags, and linked against gmodule-export-2.0.”

And because that’s a requirement, it’s encoded into the pkg-config data for the library:

```console
$ pkg-config --cflags --libs gmodule-export-2.0 |fmt
-I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include
-I/usr/include/sysprof-6 -pthread -Wl,--export-dynamic -lgmodule-2.0
-pthread -lglib-2.0

```

So if you get the pkg\_check\_modules stuff right, it’ll be taken care of automatically.

A couple of observations:

1. While it’s not _wrong_ to set up each pkg-config dependency separately, if they’re all targeted to the same executable/library, you _can_ also do them all at once for convenience — it’s called `pkg_check_moduleS()`, plural.
2. I’d suggest raising your `cmake_minimum_required()` version to the still-ancient 3.7, and telling `FindPkgConfig` to create a _target_ with the pkg-config dependencies, so that you can consume all of its results with a simple `target_link_libraries()` call.

So, something like this _should_ work:

```cmake
cmake_minimum_required(3.7 .. 3.30)

find_package(PkgConfig REQUIRED)

pkg_check_modules(
  Dependencies REQUIRED
  IMPORTED_TARGET 
  gtk4>=4.16.5 
  libadwaita-1>=1.6.1 
  gmodule-export-2.0>=2.82.2
)

```

FindPkgConfig will create a target named `PkgConfig::Dependencies` with all of the CFLAGS, linker args, and etc. set up on it, which you can use on your executable:

```cmake
add_executable(DrogonCMS ${SOURCES})

# ...after all of the dependency-finding...

target_link_libraries(
  DrogonCMS PRIVATE
  PkgConfig::Dependencies
)

```

…that replaces all of the `${[GTK4|ADW|GMODULE-EXPORTS-2.0]_INCLUDE_DIRS}`, `${..._LIBRARY_DIRS}`, `${... CFLAGS_OTHER}`, etc. you were consuming.

Note that you’ll still have to set up the _other_ dependencies on your executable target, though I’d suggest doing that with the targeted (no pun) commands instead:

```cmake
target_include_directories(
  DrogonCMS PRIVATE
    ${PostgreSQL_INCLUDE_DIRS}
    # etc...
)

```

…Or, better yet, if you up your `cmake_minimum_required()` version a bit farther to CMake 3.14 (which is still pretty old), FindPostgreSQL will create an IMPORTED target just like pkg-config. So then you just do,

```cmake
find_package (PostgreSQL)
if (TARGET PostgreSQL::PostgreSQL)
  target_link_libraries(DrogonCMS PRIVATE
    PostgreSQL::PostgreSQL
  )
else()
  pkg_check_modules(
    Sqlite3 REQUIRED
    IMPORTED_TARGET
    sqlite3>=3.43.1
  )
  # Not technically necessary because the
  # REQUIRED would cause pkg_check_modules
  # to fail if it couldn't create it, but...
  if(TARGET PkgConfig::Sqlite3)
    target_link_libraries(DrogonCMS PRIVATE
      PkgConfig::Sqlite3
    )
  endif()
endif()

```

…Oh, and as a final style note (you’re clearly working from some _very_ old `CMakeLists.txt` files as examples)… the `endif (<repeat_the_if_conditions>)` style is long deprecated. Just use `endif ()`. Same thing for `endforeach()`, `endfunction()`, etc.

I’d recommend giving [An Introduction to Modern CMake](https://cliutils.gitlab.io/modern-cmake/README.html) a read, and unlearning all of the bad old habits demonstrated by those ancient, _ancient_ `CMakeLists.txt` files.

---

_[View the full topic](https://discourse.cmake.org/t/a-gtk4-cmakelists-txt-example-that-includes-gmodule-export-2-0/13052)._
