CMake and GTK+ 3: the easy way
2015-12-08 08:26
423 查看
If you look at my GitHub repositories you will notice that I’m a big fan of
CMake and I use it in all my C projects.
Recently I started playing with GTK+ 3.0 but most of the official projects use the
autotools toolchain, so there is little documentation on how to use CMake with GTK+.
But what is CMake and why should you care?
CMake is an open-source system for managing the build process using native building environments such as
make.
I like it because it’s really easy to learn, at least compared to other build tools. It also produces a
An up-to-date Fedora 17 (yep, I changed distro again. Sigh)
CMake version 2.8.8
GNU Make version 3.82
GTK+ 3.4.4
So if you encounter any problem RTFC (Read The Funny Changelog, of course).
To compile it from the command line run:
Write a CMakeLists.txt file with the instruction on how to build your software
Make a directory where you will build your software
Run
Run
Congratulate yourself and use the time saved to troll people on the internet
We start by writing our
Then we make the build folder
And we tell cmake to build the
Finally we use
That’s it! I can’t tell you if this process works well for larger projects because I’m just getting started with GTK+ and CMake.
You should also check out some pre-made
http://francesco-cek.com/cmake-and-gtk-3-the-easy-way/
CMake and I use it in all my C projects.
Recently I started playing with GTK+ 3.0 but most of the official projects use the
autotools toolchain, so there is little documentation on how to use CMake with GTK+.
But what is CMake and why should you care?
CMake is an open-source system for managing the build process using native building environments such as
make.
I like it because it’s really easy to learn, at least compared to other build tools. It also produces a
Makefilewith a nice looking output.
Before we start
The sofware I’m using for this small guide is:An up-to-date Fedora 17 (yep, I changed distro again. Sigh)
CMake version 2.8.8
GNU Make version 3.82
GTK+ 3.4.4
So if you encounter any problem RTFC (Read The Funny Changelog, of course).
Our sample and simple program
To demostrate how to use CMake we will use the classic GTK+ Hello World.#include <gtk/gtk.h> static void activate(GtkApplication *app, gpointer user_data) { GtkWidget *window; window = gtk_application_window_new(app); gtk_window_set_title(GTK_WINDOW(window), "Hello GNOME"); gtk_widget_show_all(window); } int main(int argc, char **argv) { GtkApplication *app; int status; app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE); g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); status = g_application_run(G_APPLICATION(app), argc, argv); g_object_unref(app); return (status); }
To compile it from the command line run:
$ gcc -o hello `pkg-config --libs --cflags gtk+-3.0` hello.c $ ./hello
Enter CMake
The usual steps when using CMake are:Write a CMakeLists.txt file with the instruction on how to build your software
Make a directory where you will build your software
Run
cmake
Run
make
Congratulate yourself and use the time saved to troll people on the internet
We start by writing our
CMakeLists.txtfile in the directory containing our source.
#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}) message(staus "GTK3_INCLUDE_DIRS = ${GTK3_INCLUDE_DIRS}") link_directories(${GTK3_LIBRARY_DIRS}) message(status " GTK3_LIBRARY_DIRS = ${GTK3_LIBRARY_DIRS}") # Add other flags to the compiler add_definitions(${GTK3_CFLAGS_OTHER}) message(status " GTK3_CFLAGS_OTHER = ${GTK3_CFLAGS_OTHER}") # Add an executable compiled from hello.c add_executable(hello hello.c) # Link the target to the GTK+ libraries target_link_libraries(hello ${GTK3_LIBRARIES}) message(status "GTK3_LIBRARIES = ${GTK3_LIBRARIES}") message(status "pkgs_cflags = ${PKGS_CFLAGS}")
Then we make the build folder
$ mkdir build $ cd build
And we tell cmake to build the
Makefile
$ cmake .. # Point cmake to the folder containing CMakeLists.txt
Finally we use
maketo build our program and we run it
$ make $ ./hello
That’s it! I can’t tell you if this process works well for larger projects because I’m just getting started with GTK+ and CMake.
You should also check out some pre-made
FindGTK3.cmakemodules available, they may be better suited to your needs.
http://francesco-cek.com/cmake-and-gtk-3-the-easy-way/
相关文章推荐
- cmake clion 构建cocos2dx 应用程序并编译ios mac android
- OpenCV配置,从来没有这么简单!
- MySQL CMake参数说明手册
- CMake 使用方法
- 用 cmake 构建Qt工程(对比qmake进行学习)
- CMake 手册详解(一)
- 一起学习CMake – 01
- 理解 pkg-config 工具
- 利用 CMake 和 NDK 交叉编译 Android 下的可执行程序
- MySQL 安装 CMake Error CMAKE_CXX_COMPILER not set
- MySQL5.5安装出现CMake错误找不到CMakelists.txt原因
- 安装wxGTK-2.8.12过程中出现package not found in pkg-config
- 在vc6中使用PCRE-8.33
- vs2012下编译protobuf
- 在Qtcreator下cmake工程文件及使用Qt制作PCL C++ GUI
- CMake生成VS2012 ARM project遇到的问题
- vs2013下openSIFT的编译调试
- 使用yum 使用 CentOS-6.5-x86_64-bin-DVD1.iso 作为本地源
- 安装Geant4的过程中学到的一些东西
- Linux下opencv的交叉编译