Go to the door of Linux programming-general Linux technology-Linux programming and kernel information. The following is a detailed description. Entering the door of Linux Programming
With the increasing popularity of Linux, more and more people are using Linux. However, in Linux, how should we design programs? What kind of development tools should we use? This article will share with you some of my experiences in Linux programming.
As a GNU-compliant operating system, the open source code of Linux has become its biggest feature. We can easily obtain the source code of Linux and its application software. Linux programming is to compile or modify the program in such an environment. After learning Linux programming, you can not only compile some shared small programs for your use, you can also make some modifications to the existing software to make them more suitable for your taste.
There are many programming tools in Linux. Linux of different release versions also provides different development tools, but usually provides the C language compiler GCC on the console terminal. We only need to create a C language source program file, such as hello. c, and then run gcc hello. c. The system automatically compiles the program. If no error occurs, generate. out file. Otherwise, an error message is displayed. After generating a. out, we only need to type./a. out in the directory where it is located to run this program. However, it is inconvenient to develop such text commands. Therefore, many Linux distributions now provide an integrated development environment on the X-Windows GUI, for example, Kdevelop in blue-point Linux. These integrated environments not only provide us with a good programming interface, but also provide great convenience for programming.
When you run Kdevelop for the first time, the system automatically runs its Setup installer. This installer verifies that all files required by Kdevelop are complete and can be used after the setup. Kdevelop presents an integrated development environment similar to Visual C ++ in Linux. The left half of the Environment contains labels such as CV, LFV, and RFV, which respectively represent the class list, logical file list, and actual file list, it contains an overview of all the classes and files of the entire project. The right half of the development environment is the document display area, used to display source programs or help information.
Next we will use Kdevelop to develop two small programs and try Linux programming!
Let's first try to use it to create the simplest and most classic "Hello world !" Program, which runs on the Linux text interface, that is, the terminal. It is like the first program developed when the C language is beginner-"Hello world!" running under DOS !" Same.
Open the "project" menu and select "new ...", In the pop-up Application Wizard, select Terminal Program-C program and click Next page ".
On the second page of the wizard, enter the project name and other information, and remove the check before "generate source file and header file.
If this is not done, Application Wizard will automatically generate some source code, which happens to be the "Hello world" program we are going to do. However, to illustrate the problem, it is not convenient to figure it out, do everything by yourself. Finally, click the generate button. When "Ready" information appears on the screen, click the exit button, and our project is generated.
Then we have to input the source program. Select the "new" command in the File menu to create a "C/C ++ program" (In our example, the C program ), the system automatically generates an empty file according to the selected file header template in Application Wizard. Enter the following "classic" program. Note the case sensitivity when entering the program.
QUOTE:
# Include
Main ()
{
Printf ("Hello world! \ N ");
}
Select the "run" command in the menu or toolbar. After a period of compilation, the terminal will output "Hello world !" This sentence.
Although this program is simple, it is indeed the first program we made in the Linux environment! C and ansi c in Linux are fully compatible. It is good news for those who have learned C programming. Are you interested in Linux programming now?
However, this program is too simple. Can you create a graphic program running in X-Window? Is it very difficult to do such a program? Don't worry. Let's try it with me.
A good development kit is essential for writing graphics programs. Like the MFC class library in VC ++, Kdevelop provides a powerful Qt library and KDE library for programming in Linux. With these libraries, you can easily compile the kde gui program in X-Window. In addition, Kdevelop also provides a series of utility tools such as "dialog Editor" to facilitate development. Now let's develop a "Hello world" program for the graphic version under X-Window. We chose to use a relatively simple Qt library to write programs and try out this powerful and easy-to-use development kit.
The first step is to create a project file. The method is similar to the project file used to create the terminal program. The difference is that in the first step of Application Wizard, select "Qt Program-normal ". To illustrate the problem, in step 2 of Wizard, we also remove the check before "generate source file and header file" and write the program by ourselves. After the project file is created, create a C/C ++ program and enter the following source program:
QUOTE:
# Include
# Include
Int main (int argc, char ** argv)
{
Qapplication a (argc, argv );
QPushButton hello ("Hello world! ");
Hello. resize (100, 30 );
A. setMainWidget (& hello );
Hello. show ();
Return a.exe c ();
}
Compared with the previous C program, this program is indeed much more complicated. We will analyze it in a row. The first two lines of the program call two header files. qapplication. h is the most basic library required to build a Qt program, which is used by any Qt program. Because we want to use a button to display "Hello world !" So it also contains the header file qpushbutton. h. In the first sentence of the main program, a Qt program named a is created and the command line parameter is passed to it. In the second row, a button named hello is created and its title is "Hello world !", This enables us to display "Hello world!" on the screen !" Text wishes. The third line uses the resize method of PushButton to change the size of the hello button. The fourth line sets the main control of program a as the hello button (the word Widget can be translated as the control ). Because the new control is not displayed by default when the Qt library is used as a program, you must use the show method to display the hello button on the fifth line. The last line of the program returns the running return value of program. In this explanation, this program is not very difficult. How about, compared with the MFC class library in VC ++, the Qt library has never been inferior?
Now we can run the program. Click the "run" button on the toolbar and compile the program. After the program runs, a small window will appear on the screen, which contains a "Hello world !" Button (5), and the window can be moved or changed. However, "Hello world !" This button does not work, and clicking it does not work. How to compile an Event Response Function for Hello world? For example, what should we do if we want the "Hello world" button to be clicked and the program stops running?
In the Qt library, message responses are implemented through the connect function. Therefore, we only need to ". add a line before setMainWidget (& hello): "Qobject: connect (& hello, SIGNAL (clicked (), & a, SLOT (quit ()));. This line connects the clicked event of the hello button with the quit () function of program. So far, I believe you have a perceptual knowledge of Linux programming. Is it very interesting?
In addition, Kdevelop not only provides us with a powerful development environment, but also provides us with detailed documentation, including a series of documents such as "Programming Manual", "KDE library", and "Qt library. The "Qt library" document also contains a 14-step tutorial to describe the design method of the Qt program from the beginning, and finally complete a complete game program with us, this is a rare teaching material for getting started with Linux programming.
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