1、安装MinGW
从MinGW官网安装MinGW很麻烦,而且在线安装经常龟速容易失败,雅乐网提供了解压版的MinGW,是从codeblocks-13.12mingw-setup安装后复制出来的:
http://pan.baidu.com/s/1gd5YzVP
解压后,可以在bin目录下找到我们需要的gcc.exe ,这就是我们需要的gcc编辑器。
2、把gcc所在目录添加到环境变量
这里以把MinGW放在c盘为例,你需要修改为自己电脑中MinGW的路径。
在path变量值里添加;C:\MinGW\bin 注意分号要在英文状态
确定以后 打开cmd 输入gcc 如果有反应就ok了
3、下载gtk并设置环境变量
gtk官网:http://www.gtk.org/download/index.php
推荐使用32位的版本,下载地址:http://win32builder.gnome.org/gtk+-bundle_3.6.4-20130921_win32.zip
如果使用64位版本,结合MinGW编译的时候会有错误= =
解压后 把gtk目录放到任意位置,我以c盘根目录为例
把C:\gtk\bin 添加进环境变量
4、新建gtk.sublime-build
需要先获得gtk参数。使用pkg-config –libs –cflags gtk+-3.0命令可以获得。
在cmd窗口,执行以下命令
1 |
pkg-config --libs --cflags gtk+-3.0 >> a.txt |
然后找到a.txt 复制里面的参数(注意只有一行)。
你应该使用自己的a.txt里面的,下面的代码只适用于gtk放在c盘根目录的情况。
1 |
-mms-bitfields -IC:/gtk/include/gtk-3.0 -IC:/gtk/include/cairo -IC:/gtk/include/pango-1.0 -IC:/gtk/include/atk-1.0 -IC:/gtk/include/cairo -IC:/gtk/include/pixman-1 -IC:/gtk/include -IC:/gtk/include/freetype2 -IC:/gtk/include -IC:/gtk/include/libpng15 -IC:/gtk/include/gdk-pixbuf-2.0 -IC:/gtk/include/libpng15 -IC:/gtk/include/glib-2.0 -IC:/gtk/lib/glib-2.0/include -LC:/gtk/lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0 -lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl |
选择tool –> Build System –> New Build System
然后输入以下代码,其中用a.txt的内容替换“参数” 。注意不要有多余换行。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "working_dir": "$file_path", "cmd": "gcc -Wall $file_name -o $file_base_name 参数 -mwindows", "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "variants": [ { "name": "Run", "shell_cmd": "gcc -w $file_name -o $file_base_name 参数 -mwindows && cmd /c \"${file_path}/${file_base_name}\"" } ] } |
结果是这样的。(如果你的gtk也是放在c盘根目录下,可以直接使用下面的内容)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "working_dir": "$file_path", "cmd": "gcc -Wall $file_name -o $file_base_name -mms-bitfields -IC:/gtk/include/gtk-3.0 -IC:/gtk/include/cairo -IC:/gtk/include/pango-1.0 -IC:/gtk/include/atk-1.0 -IC:/gtk/include/cairo -IC:/gtk/include/pixman-1 -IC:/gtk/include -IC:/gtk/include/freetype2 -IC:/gtk/include -IC:/gtk/include/libpng15 -IC:/gtk/include/gdk-pixbuf-2.0 -IC:/gtk/include/libpng15 -IC:/gtk/include/glib-2.0 -IC:/gtk/lib/glib-2.0/include -LC:/gtk/lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0 -lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl -mwindows", "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "variants": [ { "name": "Run", "shell_cmd": "gcc -w $file_name -o $file_base_name -mms-bitfields -IC:/gtk/include/gtk-3.0 -IC:/gtk/include/cairo -IC:/gtk/include/pango-1.0 -IC:/gtk/include/atk-1.0 -IC:/gtk/include/cairo -IC:/gtk/include/pixman-1 -IC:/gtk/include -IC:/gtk/include/freetype2 -IC:/gtk/include -IC:/gtk/include/libpng15 -IC:/gtk/include/gdk-pixbuf-2.0 -IC:/gtk/include/libpng15 -IC:/gtk/include/glib-2.0 -IC:/gtk/lib/glib-2.0/include -LC:/gtk/lib -lgtk-3 -lgdk-3 -lgdi32 -limm32 -lshell32 -lole32 -Wl,-luuid -lpangocairo-1.0 -lpangoft2-1.0 -lfreetype -lfontconfig -lpangowin32-1.0 -lgdi32 -lpango-1.0 -lm -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl -mwindows && cmd /c \"${file_path}/${file_base_name}\"" } ] } |
保存到user目录下(Sublime Text 3\Packages\User) 文件名为 gtk.sublime-build
5、设置编译系统为gtk
6、测试
现在打开.c文件后,就可以使用tool下面的Build和Run来编译和运行了。
快捷键:
默认Ctrl + B 是build
Ctrl + Shift + B 是Run
测试程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <stdio.h> #include <gtk/gtk.h> int main(void) { gtk_init(NULL, NULL); GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_position(window, GTK_WIN_POS_CENTER_ALWAYS); gtk_window_set_title(window, "登陆"); g_signal_connect(window, "destroy", gtk_main_quit, NULL); gtk_widget_show(window); GtkGrid *grid1 = gtk_grid_new(); GtkLabel *label1 = gtk_label_new("用户名"); gtk_grid_attach(grid1, label1, 0, 0, 1, 1); gtk_widget_show(label1); GtkLabel *label2 = gtk_label_new("密码"); gtk_grid_attach(grid1, label2, 0, 1, 1, 1); gtk_widget_show(label2); GtkButton *btn1 = gtk_button_new(); gtk_button_set_label(btn1, "登陆"); gtk_grid_attach(grid1, btn1, 0, 2, 1, 1); gtk_widget_show(btn1); GtkButton *btn2 = gtk_button_new(); gtk_button_set_label(btn2, "关闭"); gtk_grid_attach(grid1, btn2, 2, 2, 1, 1); gtk_widget_show(btn2); GtkEntry *entry1 = gtk_entry_new(); gtk_grid_attach(grid1, entry1, 1, 0, 3, 1); gtk_widget_show(entry1); GtkEntry *entry2 = gtk_entry_new(); gtk_grid_attach(grid1, entry2, 1, 1, 3, 1); gtk_widget_show(entry2); gtk_widget_show(grid1); gtk_container_add(window, grid1); gtk_main(); return 0; } |