Runtime detection of GTK

Hi, ALL,
I already asked for GTK detection in configure here: Check for GTK in configure - #4 by jensgeorg.

Turns put I also need a Run-Time detection from C/C++ code.

Is best way of doing it is to use something like here: process - How do I execute a command and get the output of the command within C++ using POSIX? - Stack Overflow with pkg-config? Or is there a better way?

Probably you want to call gtk_check_version.

@jfrancis ,

I was thinking more of a “bool is_gtk_available():”…

How do I use gtk_check_version()?

Thank you.

If you can call a gtk_* function, it means you’re linking against GTK, which means GTK is available.

What are you actually trying to achieve? If you want to conditionally depend on GTK at run time then you’ll have to use dlopen() and dlsym(), but then you get to dlsym() every single GTK function.

I think you’re overcomplicating and overengineering things. If your application depends on GTK, then you get to depend on GTK.

@ebassi ,
Imagine I built my app on the system where GTK is installed.

Now the user of my application grbs the binary and tries to run it in the Qt-based environment.

What I’d like to do is to detect this situation and use Qt specific code instead.

There is no GUI calls, just basic DB communication stuff…

As you can see I’m trying to create platform-agnostic software…

Thank you.

Or I am better off checking for Qt?

That’s not how anything works.

If you’re distributing a binary, you are also on the hook for distributing its dependencies.

That’s not what “platform agnostic” means.

You should have a build for each platform, if you’re targeting multiple platforms.

If you’re worried about missing dependencies, then you should use something like Flatpak, Snap, or AppImage (with a preference for the former) to ensure you’re bringing your dependencies along.

@ebassi ,

Are you saying its not possible to build on 1 toolkit environment but run on the mixed one?

I’m targeting multiple platforms - Windows, Linux and Mac

On Windows I’m using MSVC. On Mac - Xcode.

Now on *nix there are 2 environments - GTK-based and QT-based.

Now in *nix world one can have both QT and GTK installed, right?

My program should be smart enough and check what build environment was used. I’m successfully identify whether I build under GTK or Qt and I’m using “–with-qt” to set QT dependency explicitly for configure.

Now if I’m running under environment with both libraries installed I would prefer to follow platform settings.

Lets say I have a KDE with GTK installed. Because of that I set the configure option “–with-qt”, because GTK can be unnstalled at any time. Now when I run it - how do I tell Im running under Qt?

I hope my explanation make sense. Let me know if it doesn’t.

Thank you.