"Helloworld!" in Linux !"

Source: Internet
Author: User
"Helloworld!" in Linux !" -- Linux general technology-Linux programming and kernel information. The following is a detailed description. The redhat FC has been installed for a long time. At first, it was difficult to install a software in linux. I usually conduct embedded development in windows. I know a little about some concepts in linux, and I don't know much about the rich resources on the network. After a few days, I feel that everything is troublesome in linux, and the software runs slowly. As a result, linux systems are rarely used after a hot day.

I heard that F7 was released a few days ago and I am interested again. So I wonder if the new version has been improved, and I hope I can continue my interest. No matter what you say. It took me one night to finish my work. This time, the installation of software is smooth, but the system is still relatively easy to install. In this process, an obvious experience is that the efficiency of F7 has been significantly improved, the default font is beautiful, the Chinese input method is also quite easy to use, and Stardict, etc, in short, it is much better than FC6. With a good system, it is certainly not easy to lose interest. Start to learn how to write programs in linux.

1. Editor
To write a program, you need a good editing tool. If you want to have another good reading tool, it will be even more powerful. There is SourceInsight in windows, an excellent editing tool that combines editing and reading. In Linux, I don't know if such a tool is available. The gedit of FC6 is also good, and there is also a highlighted syntax. But a colleague from Poland suggested using NEdit, which was previously developed in linux. Gedit is more inclined to edit general text, while NEdit is more suitable for editing code. As for the code tool, it has not been found. NEdit can be downloaded on its website: http://www.nedit.org. You can use SourceNavigater and SlickEdit to read the code in Linux. After trying it out, the SourceNavigater project was too slow. It took five hours to build a kernel source code project. SlickEdit uses 11.0.2, which is much faster, however, it is easy to crash, and the system can only be restarted if the system is unstable. Maybe I didn't set it up, or maybe it was because the kernel code was too large and I had to study these two tools carefully.

2. Compiler
In linux, It is GCC. Check the version.

# Gcc -- version
Gcc (GCC) 4.1.1 20061011 (Red Hat 4.1.1-30)
Copyright (C) 2006 Free Software Foundation, Inc.

This program is free software. Please refer to the source code copyright statement. This software does not have any warranty, including non-marketable and non-applicable warranties for a specific purpose.

3. Run
The compiled file can be run directly on the terminal: #./filename

We are ready to start our "Hello World !"

Create a folder named "Tests" under the user's main folder. Enter nedit in the terminal to open the editor, create a new file, and save it as the test. c file in the Tests folder. In this way, nedit can highlight the syntax Keywords of the C language. Enter the code:

Int main (int argc, char ** argv)
{
Printf ("Hello World! \ N ");
}

In VC, we have written such a small function. Just click compile and VC will compile it for us. This is because a job VC helped us to write makefile. VC knows which files we need to compile and automatically generates the makefile for compilation. In VC, we can also export makefile, that is, the file ending with. mak. In linux, We need to write makefile by ourselves to tell the compiler the rules and dependencies for GCC to compile the link file. Therefore, to compile and generate an execution file, we need to compile a makefile file for it. The makefile I reproduced earlier is useful.

In the Tests directory, create a file named makefile. Open and edit with gedit.

Test: test. o // The execution file test depends on the file test. o
Gcc-o test-g test. o // The Name Of The output file is test, and-g supports source code debug.
Test. o: test. c // test. o depends on test. c.
Gcc-c-g test. c
Clean: // defines a command clean. By default, make starts from the file header.
Rm test. o // remove the file test, test. o

Save such a makefile, open the terminal, and cd to the tests directory.

# Make

You will see:

Gcc-c-g test. c
Gcc-o test-g test. o

The Order has changed, which is the embodiment of dependency. If you don't want to see this information, you can

# Make-s

In this way, two files, test and test. o, are added to your directory.

If you want to delete these two files, we also have a clean command

# Make clean

These two files have been deleted.

Look at the results, #./test
Output Hello World! .
So far, Hello World! It's over.

When we are learning the C language, it seems that we do not need parameters when writing the main function, and the return type can also be void. The int main (int argc, char ** argv) is different here.

Verify the return type and rewrite it:

Void main (int argc, char ** argv)
{
Printf ("Hello World! \ N ");
}

Save-> make. The following message is displayed:

In the 'main' function:

Warning the return type of 'main' is not 'int'

From this we can see that the return type of the main function here should be int, otherwise there will be warning;

Rewrite:

Int main (int argc, char ** argv)
{
Printf ("Hello World! \ N ");
Printf ("argc = % d \ n", argc );
Printf ("argv [0] = % s \ n", argv [0]);
Printf ("argv [1] = % s \ n", argv [1]);
}

Save-> make, compiled.

#./Test Hello_World

Result output:
Hello World!
Argc = 2
Argv [0] =./test
Argv [1] = Hello_world

The result shows the function of the main parameter. This usage is rarely mentioned when learning C. If you have been writing a program in windows, you may rarely notice this usage. This is required when writing some small tools running under DOS.
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.