一、简介

        Gtk+2.0的动态链接库的创建与C语言相似,使用动态链接库可以有效的减小可执行程序的大小,在linux下应用广泛(linux系统的动态库存放在/lib、/usr/lib、/usr/local/lib等目录下)

二、详解

1、动态链接库文件

hello.c文件:
#include <gtk/gtk.h>
void hello(void)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "您好!\n这是动态链接库测试");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void create_info_dialog(gchar *message)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_INFO, GTK_BUTTONS_OK, message);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}
编译(生成动态库libhello.so):
gcc -fPIC -shared -o libhello.so hello.c `pkg-config --cflags --libs gtk+-2.0`

2、使用动态链接库

main.cpp文件:

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *button;
    GtkWidget *label;
    GtkWidget *entry;
    PangoFontDescription *desc;
    static GdkColor red = {0, 0xffff, 0, 0};
    static GdkColor green = {0, 0, 0xffff, 0};
    static GdkColor blue = {0, 0, 0, 0xffff};
    static GdkColor yellow = {0, 0xffff, 0xffff, 0};
    static GdkColor cyan = {0, 0, 0xffff, 0xffff};
    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
    gtk_window_set_title(GTK_WINDOW(window), "设定控件的样式");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_container_set_border_width(GTK_CONTAINER(window), 15);

    desc = pango_font_description_from_string("Simhei 24");
    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), vbox);
    button = gtk_button_new_with_label("绿色的按钮");
    gtk_widget_modify_bg(button, GTK_STATE_NORMAL, &green);
    gtk_widget_modify_bg(button, GTK_STATE_ACTIVE, &cyan);
    gtk_widget_modify_bg(button, GTK_STATE_PRELIGHT, &red);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 8);

    button = gtk_button_new_with_label("红色字的按钮");
    gtk_widget_modify_fg(GTK_BIN(button)->child, GTK_STATE_NORMAL, &red);
    gtk_widget_modify_fg(GTK_BIN(button)->child, GTK_STATE_ACTIVE, &blue);
    gtk_widget_modify_fg(GTK_BIN(button)->child, GTK_STATE_PRELIGHT, &green);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 8);
    label = gtk_label_new("蓝色的标签文字\n主要是改变了前景颜色");
    gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &blue);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 8);

    desc = pango_font_description_from_string("Simsun 24");
    entry = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entry), "红色的输入文字");
    gtk_widget_modify_font(entry, desc);
    gtk_widget_modify_text(entry, GTK_STATE_NORMAL, &red);
    gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 8);
    entry = gtk_entry_new();
    gtk_widget_modify_font(entry, desc);
    gtk_entry_set_text(GTK_ENTRY(entry), "绿色的背景");
    gtk_widget_modify_base(entry, GTK_STATE_NORMAL, &green);
    //gtk_widget_modify_text(entry, GTK_STATE_NORMAL, &red);
    gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 8);

    gtk_widget_show_all(window);
    gtk_main();
    return TRUE;
}
编译:
gcc -o main main.c `pkg-config --cflags --libs gmodule-2.0 gtk+-2.0`
运行:
   

也可以编写一个makefile文件:

CC=gcc
all:
	$(CC) `pkg-config --cflags gtk+-2.0` -fPIC -DPIC -c hello.c -o hello.lo
	$(CC) `pkg-config --libs gtk+-2.0` -shared hello.lo -o libhello.so
	$(CC) main.c -o main `pkg-config --cflags --libs gmodule-2.0 gtk+-2.0`

clean:
	rm -rf *.o *.lo *.a *.la .libs

三、总结

(1)生成动态链接库的参数是-fPIC和-shared,glib中使用动态链接库的是gmodule,g_module_open打开动态链接库名,g_module_symbol取得动态链接库的函数指针,接着直接调用此函数即可。
(2)若有建议,请留言,在此先感谢!

Logo

更多推荐