Skip to content

Instantly share code, notes, and snippets.

@SAW4
Last active March 6, 2024 11:58
Show Gist options
  • Save SAW4/c15f8a296a98ae5667ff3473f1029e50 to your computer and use it in GitHub Desktop.
Save SAW4/c15f8a296a98ae5667ff3473f1029e50 to your computer and use it in GitHub Desktop.
C++ Project, with CLion + CMake + Pkg-config. Use simple gtk application as example.

How to create a C++ GTK project with Cmake + Pkg-config

  1. Create a project with CLion (Click C++ Executable)
  2. Create your source code directory src
  3. Create main.cpp and CMakeLists.txt in src

Content of src/main.cpp:

#include <gtkmm.h>

int main(int argc, char *argv[])
{
  auto app =
    Gtk::Application::create(argc, argv,
      "org.gtkmm.examples.base");

  Gtk::Window window;
  window.set_default_size(200, 200);

  return app->run(window);
}

Content of src/CMakeLists.txt:

set(SOURCE_FILES main.cpp)
link_directories(${GTKMM_LIBRARY_DIRS})
include_directories(${GTKMM_INCLUDE_DIRS})
add_executable(example main.cpp)
target_link_libraries(example ${GTKMM_LIBRARIES})

Content of CMakeLists.txt (be careful, it is the outer most CMakeLists.txt):

cmake_minimum_required(VERSION 3.8)
project(example)
set(CMAKE_CXX_STANDARD 11)
find_package(PkgConfig)
pkg_check_modules(GTKMM gtkmm-3.0)
add_subdirectory(src)
  1. Select the outer CMakeLists.txt, then click the green arrow icon to rebuild the lib symbols.
  2. Select main.cpp to compile your program, done.

Project Structure (Simpify)

➜  example tree
.
├── cmake-build-debug  <---- cmake generated stuffs (usually no need to worry about it)
├── CMakeLists.txt     <---- outer CMakeLists.txt
└── src                <---- source code directory
    ├── CMakeLists.txt
    └── main.cpp

Reference

Project structure

When you create the project with CLion, the original structure should look like this:

.
├── cmake-build-debug
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.8.2
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── a.out
│   │   │   │   ├── CMakeCCompilerId.c
│   │   │   │   └── tmp
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       ├── CMakeCXXCompilerId.cpp
│   │   │       └── tmp
│   │   ├── clion-environment.txt
│   │   ├── clion-log.txt
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── example.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   └── progress.make
│   │   ├── feature_tests.bin
│   │   ├── feature_tests.c
│   │   ├── feature_tests.cxx
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── progress.marks
│   │   └── TargetDirectories.txt
│   ├── cmake_install.cmake
│   ├── example.cbp
│   └── Makefile
├── CMakeLists.txt
└── main.cpp

9 directories, 34 files

Which cmake-build-debug directory is auto created and we dont need to worry about it; we just need to take care to the CMakeLists.txt in the outer directory.

Its original content might be:

cmake_minimum_required(VERSION 3.8)
project(example)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(example ${SOURCE_FILES})

After finsish the setting, the project structure should look like this:

➜  example tree
.
├── cmake-build-debug
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.8.2
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── a.out
│   │   │   │   ├── CMakeCCompilerId.c
│   │   │   │   └── tmp
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       ├── CMakeCXXCompilerId.cpp
│   │   │       └── tmp
│   │   ├── clion-environment.txt
│   │   ├── clion-log.txt
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── example.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── CXX.includecache
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.internal
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   ├── main.cpp.o
│   │   │   └── progress.make
│   │   ├── feature_tests.bin
│   │   ├── feature_tests.c
│   │   ├── feature_tests.cxx
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── progress.marks
│   │   └── TargetDirectories.txt
│   ├── cmake_install.cmake
│   ├── example
│   ├── example.cbp
│   ├── Makefile
│   └── src
│       ├── CMakeFiles
│       │   ├── CMakeDirectoryInformation.cmake
│       │   ├── example.dir
│       │   │   ├── build.make
│       │   │   ├── cmake_clean.cmake
│       │   │   ├── CXX.includecache
│       │   │   ├── DependInfo.cmake
│       │   │   ├── depend.internal
│       │   │   ├── depend.make
│       │   │   ├── flags.make
│       │   │   ├── link.txt
│       │   │   ├── main.cpp.o
│       │   │   └── progress.make
│       │   └── progress.marks
│       ├── cmake_install.cmake
│       ├── example
│       └── Makefile
├── CMakeLists.txt
└── src
    ├── CMakeLists.txt
    └── main.cpp

13 directories, 54 files
@mvcaaa
Copy link

mvcaaa commented Feb 26, 2018

this helps, ty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment