I have been using the Clion IDE and am trying to get a simple GTK program to compile using it. I have found that Clion uses CMake, so the issues is here rather than with the IDE itself. I am able to successfully compile and run the program directly from the terminal but have been unsuccessful using CMake.
The problem is simple: when I attempt to compile, the compiler cannot find gtk.h, which is located in /usr/include/gtk-3.0/gtk/gtk.h
. I have found that somehow the command compiler argument 'pkg-config --libs --cflags gtk+-3.0'
fixes this problem but I have been unable to add this argument using CMake.
I have tried:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} `pkg-config --libs --cflags gtk+-3.0`")
But am met with:
Linking CXX executable test
c++: error: `pkg-config: No such file or directory
c++: error: gtk+-3.0`: No such file or directory
c++: error: unrecognized command line option ‘--libs’
c++: error: unrecognized command line option ‘--cflags’
make[3]: *** [test] Error 1
make[2]: *** [CMakeFiles/test.dir/all] Error 2
make[1]: *** [CMakeFiles/test.dir/rule] Error 2
make: *** [test] Error 2
Any suggestions?
Further research has revealed this tutorial precisely on the issue I am having. It works like a charm but appears to throw many seemingly undefined variables into the mix. Can anyone explain how and why this works?
# Set the name and the supported language of the project
project(hello-world C)
# Set the minimum version of cmake required to build this project
cmake_minimum_required(VERSION 2.6)
# Use the package PkgConfig to detect GTK+ headers/library files
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
# Add other flags to the compiler
add_definitions(${GTK3_CFLAGS_OTHER})
# Add an executable compiled from hello.c
add_executable(hello main.c)
# Link the target to the GTK+ libraries
target_link_libraries(hello ${GTK3_LIBRARIES})
Use the FindPkgConfig module
the bits in chevrons (
<...>
) need to be replaced with real valuesyou can find out more with
Basically the
include(FindPkgConfig)
line brings in a few macros - it also ensures that pkg-config is available in the environment. Then a call topkg_check_modules
effectively runspkg-config
, parses the output, and creates a suite of variables using the first argument as a stem.From the help, this is a basic list (XPREFIX is usually the stem you supply)
You have to understand that when you manually type the compilation command
is not actually an argument to the compiler but makes the shell execute pkg-config with arguments and uses the output of this execution being used as commands to the compiler. I suggest you type just the pkg-config subcommand into your shell to see what it outputs. E.g. on my laptop it is
and these are the actual arguments passed on to the compiler.
CMake is not a shell through. It has it's own package detection and configuration mechanism. See @kdopen's answer for the details.