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, too.

2333.

The general requirements for a junior class are written in C, even if you learn the object-oriented very well. Can only be honest to write in C.

There is, of course, much to be said in this.

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 already very little to do things on windows. Write in C, and want to write a little taller. Of course we have a graphical interface. Write the interface program on Linux with C. GTK + is indispensable, but using object-oriented thinking, assume that the code is mixed with too many interfaces. When you write to thousands of lines, you can't tell 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 you better show your thoughts.

So, use Glade to draw the interface. Write the main algorithm logic in the code so that the interface program and the main algorithm logic can be separated very clearly. Make the development process faster and reduce the hassle of writing the interface at the same time. At the same time with GTK + to write some of the C language implementation of the interface to connect the program, convenient Galde draw the interface to better work.

All right, that's all. First 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.

In the above editing program I do not have a practical Linux vim to write, one is not very familiar with, at the same time, write a larger project words, Vim always think is not too suitable.

Environment configuration:

1, glade installation can go to Ubuntu Software Center search installation. This is the simplest.

can also be used in 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, assuming that the GCC version number is lower. Ability to update the version number of GCC. The version number of GTK + is 3.0 or higher.

After installing the above procedures, it is possible to check if all 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 able to execute.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahvzdf9zdxblcm1hbg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/center ">

You can also enter Glade on the command line at the same time, and the same will open the Glade program.

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, enter the following compile command into the directory where you saved the above file on the command line. 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 execute the generated HelloWorld program. </span>
3. Make Test

Enter directly on the command line

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

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahvzdf9zdxblcm1hbg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/center ">
All right, the environment above has been built. Be able to run one of the simplest test procedures.

Demo test.

Create a new main.c file in the editor. Code such as the following

<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 form.  The message above is 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 form Gtk_dialog_run (Gtk_dialog (dialog));//  Executes the form created above Gtk_widget_destroy (dialog);//Releases the form's 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 in the following write 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 form Gtk_container_set_border_width (Gtk_container (Win), 8);//Set the size of the form Edge Gtk_window_set_title (Gtk_window (Win), "Hello World ");//Set the form title Gtk_window_set_position (Gtk_window (Win), gtk_win_pos_center);//Set the form position gtk_widget_realize (win);//  Implement the above form G_signal_connect (win, "destroy", Gtk_main_quit, NULL);//form signal connection, click the Close button is to close the form. /* 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 form 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 a specific gaze. 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>
Executes the executable file.

The following interface will appear.


Click on the message and the following HelloWorld information interface will appear.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahvzdf9zdxblcm1hbg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/center ">

Well, by the end of all the work here, the main program will be developed.

Of course. It also requires you to learn some other knowledge. For example, 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 run some interface information in the code.

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

These are just some of the things I will be using in my class to explain, of course, limited capacity. In the above and later to speak of inappropriate words, welcome to criticize correct.

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)

Related Article

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.