CMake 编译gtk文件
CMakeLists.txt也可以通过读取配置文件(pkgconfig下的pc文件 )编译程序,如编译gtk时,通常需要编译就选项`pkg-config --cflags --libs gtk+-2.0`,而在CMakeLists.txt中却不能那么写,下面看如何用CMake调用配置文件对代码进行编译,
一个简单gtk代码如下,main.c:
-
#include <stdlib.h>
-
#include <gtk/gtk.h>
-
-
void selection_received( GtkWidget *widget,
-
GtkSelectionData *selection_data,
-
gpointer data );
-
-
/* 当用户点击"Get Targets"按钮时调用的信息处理函数 */
-
void get_targets( GtkWidget *widget,
-
gpointer data )
-
{
-
static GdkAtom targets_atom = GDK_NONE;
-
GtkWidget *window = (GtkWidget *)data;
-
-
/* 得到"TARGETS"字符串对应的原子 */
-
if (targets_atom == GDK_NONE)
-
targets_atom = gdk_atom_intern ("TARGETS", FALSE);
-
-
/* 要求获取主选中区的"TARGETS"形式的目标 */
-
gtk_selection_convert (window, GDK_SELECTION_PRIMARY, targets_atom,
-
GDK_CURRENT_TIME);
-
}
-
-
/* 当选中区所有者返回数据时调用的信号处理函数 */
-
void selection_received( GtkWidget *widget,
-
GtkSelectionData *selection_data,
-
gpointer data )
-
{
-
GdkAtom *atoms;
-
GList *item_list;
-
int i;
-
-
/* **** 重要 **** 检测获取信息是否成功 */
-
if (selection_data->length < 0)
-
{
-
g_print ("Selection retrieval failed\n");
-
return;
-
}
-
/* 确认得到的数据为原来要求的形式 */
-
if (selection_data->type != GDK_SELECTION_TYPE_ATOM)
-
{
-
g_print ("Selection \"TARGETS\" was not returned as atoms!\n");
-
return;
-
}
-
-
/* 打印接收到的原子 */
-
atoms = (GdkAtom *)selection_data->data;
-
-
item_list = NULL;
-
for (i = 0; i < selection_data->length / sizeof(GdkAtom); i++)
-
{
-
char *name;
-
name = gdk_atom_name (atoms[i]);
-
if (name != NULL)
-
g_print ("%s\n",name);
-
else
-
g_print ("(bad atom)\n");
-
}
-
-
return;
-
}
-
-
int main( int argc,
-
char *argv[] )
-
{
-
GtkWidget *window;
-
GtkWidget *button;
-
-
gtk_init (&argc, &argv);
-
-
/* 创建顶级窗口 */
-
-
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-
gtk_window_set_title (GTK_WINDOW (window), "Event Box");
-
gtk_container_set_border_width (GTK_CONTAINER (window), 100);
-
-
g_signal_connect (G_OBJECT (window), "destroy",
-
G_CALLBACK (exit), NULL);
-
-
/* 创建一个按钮,用户按它以获取目标 */
-
-
button = gtk_button_new_with_label ("Get Targets");
-
gtk_container_add (GTK_CONTAINER (window), button);
-
-
g_signal_connect (G_OBJECT (button), "clicked",
-
G_CALLBACK (get_targets), window);
-
g_signal_connect (G_OBJECT (window), "selection_received",
-
G_CALLBACK (selection_received), NULL);
-
-
gtk_widget_show (button);
-
gtk_widget_show (window);
-
-
gtk_main ();
-
-
return 0;
-
}
下面为用于编译这个Gtk程序的CMakeLists.txt文件
FIND_PACKAGE(GTK)
IF (GTK_FOUND)
INCLUDE_DIRECTORIES(${GTK_INCLUDE_DIR})
ADD_DEFINITIONS(-DHAVE_GTK)
MESSAGE("GTK 1.x found and used as GUI\n")
ELSE (GTK_FOUND)
PKGCONFIG(gtk+-2.0 GTK2_INCLUDE_DIR GTK2_LINK_DIR GTK2_LINK_FLAGS GTK2_CFLAGS)
PKGCONFIG(gthread-2.0 GTHREAD_INCLUDE_DIR GTHREAD_LINK_DIR GTHREAD_LINK_FLAGS+- GTHREAD_CFLAGS)
IF (GTK2_INCLUDE_DIR)
SET(CMAKE_C_FLAGS "${GTK2_CFLAGS} ${GTHREAD_CFLAGS}")
STRING(REGEX REPLACE "-l" "" GTK_LIBRARIES "${GTK2_LINK_FLAGS} ${GTHREAD_LINK_FLAGS}")
SEPARATE_ARGUMENTS(GTK_LIBRARIES)
ADD_DEFINITIONS(-DHAVE_GTK2)
MESSAGE("GTK 2.x found and used as GUI\n")
ELSE (GTK2_INCLUDE_DIR)
MESSAGE(FATAL_ERROR "GTK or GTK2 is required to build this project.")
ENDIF (GTK2_INCLUDE_DIR)
ENDIF (GTK_FOUND)
ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main ${GTK2_LINK_FLAGS})
上面的文件首先看是否有gtk的配置文件,如果有则调用gtk的配置文件进行编译,否则查看gtk2.0的配置文件。如果想直接用gtk2.0的配置编译选项,则上面的CMakeLists.txt可改成下面内容:
-
INCLUDE(UsePkgConfig)
-
-
FIND_PACKAGE(GTK)
-
-
PKGCONFIG(gtk+-2.0 GTK2_INCLUDE_DIR GTK2_LINK_DIR GTK2_LINK_FLAGS GTK2_CFLAGS)
-
PKGCONFIG(gthread-2.0 GTHREAD_INCLUDE_DIR GTHREAD_LINK_DIR GTHREAD_LINK_FLAGS+- GTHREAD_CFLAGS)
-
-
IF (GTK2_INCLUDE_DIR)
-
SET(CMAKE_C_FLAGS "${GTK2_CFLAGS} ")
-
STRING(REGEX REPLACE "-l" "" GTK_LIBRARIES "${GTK2_LINK_FLAGS} ")
-
SEPARATE_ARGUMENTS(GTK_LIBRARIES)
-
ADD_DEFINITIONS(-DHAVE_GTK2)
-
ELSE (GTK2_INCLUDE_DIR)
-
MESSAGE(FATAL_ERROR "GTK or GTK2 is required to build this project.")
-
ENDIF (GTK2_INCLUDE_DIR)
-
-
ADD_EXECUTABLE(main main.c)
-
TARGET_LINK_LIBRARIES(imain ${GTK2_LINK_FLAGS})
接下来的事就是
$> cmake .
$> make
$> ./main
下面简单介绍第二个CMakeLists.txt中的代码
第1行 包含配置文件模块;
第3行 查找gtk配置文件;
第5行 用配置文件gtk+-2.0对几个变量GTK2_INCLUDE_DIR、GTK2_LINK_DIR、GTK2_LINK_FLAGS、GTK2_CFLAGS赋值;
第6行用配置文件gthread-2.0对几个变量GTHREAD_INCLUDE_DIR GTHREAD_LINK_DIR GTHREAD_LINK_FLAGS+- GTHREAD_CFLAGS赋值;
第8行判断GTK2_INCLUDE_DIR是否有值;
第9行用变量GTK2_CFLAGS的值对CMAKE_C_FLAGS赋值,其中为有编译选项;
第10行将变量GTK2_LINK_FLAGS中的"-l"用空格“”替换;
第11行将变量GTK_LIBRARIES中的所有空格用分号“;”替换;
第12行将 -D定义的选项添加到C或者C++编译行中;
第14行如果GTK2_INCLUDE_DIR没有值,则输出错误信息:
"GTK or GTK2 is required to build this project."
第17行用于将源文件main.c生成可执行文件main;
第18行指定程序运行时所需要的库。
2008年5月06日 06:19 您好!我想问问GTHREAD_LINK_FLAGS+-这个加减号是什么意思呢?谢谢您的好帖子!
2008年5月20日 16:55 不好意思,我也不知道。。。