用cmake构建gtk程序
情况说明
先前已经在windows下基于GDI实现了一个简陋的imshow:基于GDI的imshow:使用stb_image读取图像并修正绘制。考虑跨平台,也考虑万一某天M$不让我们用盗版系统了,还是需要实现一个Linux/Mac下的imshow。
GTK相当于GDI在Linux下的一个替代。我用的是GTK-3
官方GTK文档的tutorial给出了一个能运行的例子,不过构建工具用的非常raw的gcc和pkg-config,未免过于刀耕火种。用CMake改造一下也是可以的。这里简单记录。
依赖项
ubuntu下的依赖项
- pkg-config
sudo apt install pkg-config
- gtk3
sudo apt-get install libgtk-3-dev
- cmake
手动下载解压配置最新的cmake,例如3.14
mac OSX下的依赖项
mac下也可以使用gtk-3.0。依赖问题解决如下:
brew install pkg-config gtk+3 libffi
然后还需要手动解决下libffi无法被cmake找到的问题,第一种办法是在/.bashrc或/.zshrc中添加:
export PKG_CONFIG_PATH="(brew --prefix libffi)/lib/pkgconfig/"
不过这个办法略显ugly,另一种ugly的做法是在CMake中临时修改PKG_CONFIG_PATH环境变量。考虑到gtk库本身的链接目录在Mac OSX下也不能被cmake找到,还是需要另行写一段cmake的,因此不如都放到cmake脚本中处理。
目录结构
CMakeLists.txt
src/main.c
build/
代码
CMakeLists.txt
试了下直接find_package(GTK)
不行。需要搭配pkg-config,cmake已经对此做出封装。
cmake_minimum_required (VERSION 3.14)
project (hello-gtk)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
message(STATUS "----- GTK3_INCLUDE_DIRS: ${GTK3_INCLUDE_DIRS}")
message(STATUS "----- GKT3_LIBRARIES: ${GTK3_LIBRARIES}")
message(STATUS "----- GTK3_LINK_LIBRARIES: ${GTK3_LINK_LIBRARIES}")
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
list(APPEND FC_DEP_LIBS ${GTK3_LIBRARIES})
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
#export PKG_CONFIG_PATH="$(brew --prefix libffi)/lib/pkgconfig/"
set(ENV{PKG_CONFIG_PATH} /usr/local/opt/libffi/lib/pkgconfig)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
message(STATUS "----- GKT3_LIBRARIES: ${GTK3_LIBRARIES}")
message(STATUS "----- GTK3_LINK_LIBRARIES: ${GTK3_LINK_LIBRARIES}")
include_directories(${GTK3_INCLUDE_DIRS})
list(APPEND FC_DEP_LIBS ${GTK3_LINK_LIBRARIES})
endif()
add_executable (gtk-test src/main.c)
#add_definitions (${GTK3_CFLAGS_OTHER})
message(STATUS "----- ${GTK3_CFLAGS_OTHER}")
# -pthread
target_link_libraries (gtk-test ${FC_DEP_LIBS})
main.c
#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), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
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;
}
Linux下cmake ..执行结果:
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'gtk+-3.0'
-- Found gtk+-3.0, version 3.18.9
-- ----- -pthread
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chris/dev/libfc/gtk/build
make之后运行得到窗口:
reference
How do I link gtk library more easily with cmake in windows?
Greatness is never a given, it must be earned.