Using glade and GTK + to develop C-language interface program under Ubuntu (i)

Source: Internet
Author: User
Tags gtk

Preface: For the University of Computer Department of the annual summer vacation lesson has too much to say, can learn a lot of things, of course, do not rule out soy sauce, these can pass oh, everything is soy sauce, life is soy sauce bar. 2333.

For a junior class before the general requirements are written in C, even if you learn the object-oriented very good, can only be honest to write in C. There is, of course, much more to be said.

Write in C, in the face of windows that can no longer spit the groove of the cmd terminal, I will not say too much, because it has rarely been to the windows to do things. Write in C, and want to write the tall on a point, of course, to have a graphical interface, in Linux with C write interface program, GTK + is not necessary, but the use of object-oriented thinking, if the code is mixed with too many interfaces of things, feel you write to thousands of lines, you will not be out there is the main logic, There is the interface program. In the case of MVC, you have to put the main logic in your main algorithm, the interface to help better show your thoughts. Then, using Glade to draw the interface, write the main algorithm logic in the code, so that the interface program and the main algorithm can be clearly separated from the logic. Make the development process faster and also reduce the hassle of writing interfaces. At the same time, with GTK + to write some of the C language implementation of the interface connection program, convenient Galde draw the interface can better work.

Well, to say so much, first to introduce the main things to use.

Main environment: ubuntu14.04, of course, 12.04 can also.

Tools to use in development: GCC compilers, make commands, Glade development interface, GTK + installation, an IDE for editing programs.

The above editing program I did not use the Linux vim to write, one is not very familiar with, at the same time, write a relatively large project, Vim always feel not too suitable.

Environment configuration:

1, Glade installation can go to Ubuntu Software Center search installation. This is the simplest. can also be used at the command line

<span style= "FONT-SIZE:18PX;" >sudo Apt-get Install glade</span>
To install.

2. Make installation

Enter directly on the command line

<span style= "FONT-SIZE:18PX;" >sudo Apt-get Install make</span>
To install.

3, for GCC and GTK +, generally installed Linux has its own gcc and GTK +, this can not be installed, if you feel that the GCC version is relatively low, you can update the GCC version. The GTK + version is more than 3.0.

After the installation of the above procedures, you can check whether it can work properly.

1, for Galde, press win key, enter Glade in the interface that appears, click Glade icon, appear the following interface explanation Galde is can run.


You can also enter Glade on the command line, and the Glade program will open as well.

2, the GCC test can be the simplest C file to test.

Create a new HELLOWORLD.C program in the editor.

<span style= "FONT-SIZE:18PX;" > #include <stdlib.h> #include <stdio.h>int main (int argc, char *argv[]) {     printf ("helloworld!");     return 0;} </span>
After saving the file, on the command line, go to the folder where you saved the above file, and enter the following compile command to compile the above HELLOWORLD.C file

<span style= "FONT-SIZE:18PX;" >GCC Helloworld.c-o helloworld</span>
After compiling the above command, the executable file will be generated, HelloWorld, input

<span style= "FONT-SIZE:18PX;" >./helloworld to run the generated HelloWorld program. </span>
3. Make Test

Enter directly on the command line

<span style= "FONT-SIZE:18PX;" >make--version</span>
You can see the version of make.


OK, the above environment is built, you can run one of the simplest test procedures.

Demo test.

Create a new main.c file in the editor with the following code

<span style= "FONT-SIZE:14PX;" > #include <stdlib.h> #include the callback function of the <gtk/gtk.h>//helloworld button, That is, pressing the HelloWorld button will perform this helloWorld function static void HelloWorld (Gtkwidget *wid, Gtkwidget *win) {Gtkwidget *dialog = null;//gt  Controls in K//Create a dialog window with the information above as Hello world! Dialog = Gtk_message_dialog_new (Gtk_window (Win), Gtk_dialog_modal, Gtk_message_info, Gtk_buttons_close, "Hello World!")  ; Gtk_window_set_position (Gtk_window (dialog), gtk_win_pos_center);//Set the position of the window Gtk_dialog_run (Gtk_dialog (dialog));//  Run the window created above Gtk_widget_destroy (dialog);//Release window interface memory}int main (int argc, char *argv[]) {gtkwidget *button = NULL;  Gtkwidget *win = NULL;  Gtkwidget *vbox = NULL; /* Initialize GTK + *///initialize GTK + code, these three lines of code do not move, your code is written below G_log_set_handler ("GTK", G_log_level_warning, (Glogfunc) gtk_  False, NULL);  Gtk_init (&AMP;ARGC, &AMP;ARGV);  G_log_set_handler ("Gtk", G_log_level_warning, G_log_default_handler, NULL); /* Create the main window */win = gtk_window_new (gtk_window_toplevel);//new out aMain Window Gtk_container_set_border_width (Gtk_container (Win), 8);//Set the size of the window Edge Gtk_window_set_title (Gtk_window (Win), "Hello ///Set window title Gtk_window_set_position (Gtk_window (Win), gtk_win_pos_center);//Set window position gtk_widget_realize (win);//  Implement the above window G_signal_connect (win, "destroy", Gtk_main_quit, NULL);//Window signal connection, click the Close button is to close the window. /* Create a vertical box with buttons */VBox = Gtk_vbox_new (TRUE, 6); Creating a VBox Container Gtk_container_add (Gtk_container (Win) , vbox);//Add the created container to the main window created above button = Gtk_button_new_from_stock (gtk_stock_dialog_info);//Create a button G_signal_connect (g_  OBJECT (Button), "clicked", G_callback (HelloWorld), (Gpointer) win);//Set the button's callback function, see CALLBACK Everyone should know that this is a callback function. Gtk_box_pack_start (Gtk_box (vbox), button, True, True, 0);//Add the Created button to the vbox above//create the Close button below, and set the callback function of the Close button to the eject function of the system Gtk_  Main_quit.  button = Gtk_button_new_from_stock (Gtk_stock_close);  G_signal_connect (Button, "clicked", Gtk_main_quit, NULL);  Gtk_box_pack_start (Gtk_box (vbox), button, True, True, 0); /* Enter the main loop*/Gtk_widget_show_all (WIN);//Display the above created interface Gtk_main ();//Start the entire main loop. return 0;} </span>
The above code has made detailed comments, it should not be difficult to understand.

After saving the above main.c file, enter it on the command line

<span style= "FONT-SIZE:18PX;" >gcc-wall-g-o main main.c ' pkg-config--cflags--libs gtk+-3.0 '-export-dynamic</span>

Compiles the main.c file and generates the executable file, main.

Continue Typing

<span style= "FONT-SIZE:18PX;" >./main</span>
Run the executable file. The following interface appears.


Click on the message and the HelloWorld Information screen will appear below.


Well, all the work is done here, and then the main program can be developed. Of course, this also requires you to learn some other knowledge, such as how to draw the interface you want in glade, how to connect the interface signal and the actual function. And how to correlate these interfaces in your code, and how to perform some interface information in your code.

How to use make to make compiling commands easier and smarter.

These are just some of my lessons to be used to explain, of course, after all, limited capacity, in the above and after the improper words, welcome criticism.

Respect the original, welcome reprint, reproduced please indicate the source: http://write.blog.csdn.net/postedit/38944891


Using glade and GTK + to develop C-language interface program under Ubuntu (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.