From windows to Linux-programming-cygwin, develop Linux programs in win

Source: Internet
Author: User
Tags system echo

From windows to Linux -- programming -- cygwin, develop Linux programs in win
Qian Kun smile [smileonce] smileonce@126.com 2004-7-23
Copyright reprinted please indicate source http://blog.csdn.net/smileonce

Many Windows users are not used to Linux development environments. Although I would like to try it out, I am often afraid that the Linux system will disrupt my normal life: 1> install the Linux system and break down the windows system, resulting in my failure to live normally; 2> Linux development is too difficult to get started. writing the first helloworld is no less difficult than Java, and the Environment configuration is hard to understand. My opinion on this is: whether the road is flat or steep, you will always go. If you are willing to invest in the Linux development community, you will not install the Linux system, it cannot be imagined that the work environment will not be configured. (In fact, you really need to understand the principles of a lot of things, otherwise it is difficult to troubleshoot: for example, disk boot principle, partition structure principle, LINUX partition structure and file system, environment variable settings, a wide variety of compressed package installation package unzip installation method, user management permission management and other common commands, so that the driver installation system, culture, and so on, are trivial ).

This article tries to skip this difficult step and enable a Linux simulator in a win environment (which is basically the same as the command line development environment in Linux). In just 20 minutes, it teaches you how to make a pure helloworld compiled by GCC in Linux. It's like trying it before buying a snack. Isn't it a pleasant thing? (Note: In fact, cygwin not only has this simulation function, but it is also a very effective tool for porting UNIX <--> win programs, and someone uses it for Embedded System Development)

1. What is cygwin?
To answer this question, you 'd better Google "cygwin's history" and perhaps get a more detailed answer. In short, cygwin is a Linux Simulation Environment running on the Windows platform. It is a free software developed by Cygnus solutions. (The company has developed many good things, famous for its ECOs, but it is now acquired by RedHat ). Many friends do not understand the differences and connections between Linux and Unix. I would like to give a brief introduction here. UNIX is a registered trademark. It is an operating system that can be authorized to use only when a large number of conditions are met and a considerable fee is paid. Linux is a clone version of UNIX, it is an operating system created by its founder Linus and many world-renowned hackers. Why is there a lot of software between Linux and UNIX that can be easily transplanted? Because Linux also meets the POSIX specifications, the operating mechanism is similar to Unix.
Here is a reference to the Internet (Source: http://blog.csdn.net/glock18/archive/2004/07/10/38275.aspx ),
It is used to explain how cygwin works: Cygnus first improved GCC, GDB, gas and other development tools so that they can generate and explain Win32 target files. Then they will port these tools to the Windows platform. One solution is to greatly modify the source code of these tools based on Win32 APIs, which obviously requires a lot of work. Therefore, they adopt a different method-they write a shared library (cygwin DLL), and call the Unix-style calls (such as fork, spawn, signals, select, sockets, and so on) are encapsulated. That is to say, they write a simulation layer for UNIX system libraries based on Win32 APIs. In this way, as long as the source code of these tools and the shared library are connected together, you can use the cross compiler on the unix host to generate a tool set that can run on the Windows platform. Based on these development tools transplanted to the Windows platform, Cygnus gradually uses other tools (almost no need to modify the source code, but only need to modify their configuration scripts) the software is transplanted to Windows. In this way, running bash, development tools, and user tools on Windows seems to work on UNIX. For a more detailed description of cygwin implementation, please refer to the http://cygwin.com/cygwin-ug-net/highlights.html

Ii. Install cygwin.
The Installation File of cygwin can be easily found by Google. At present, there are two types of websites in China: "network installation version" and "Local installation version. The standard release version should be the network installation version. There is no difference between the two. The following describes the installation process.

Step 1. Click the Installation File (setup.exe) after the download. the first screen is the GNU copyright description. Click "next (N)-> ",
Go to the installation mode selection screen.

Step2. the installation modes include "Install from Internet", "Download Form Internet ",
"Install from local directory. "Install form Internet" is installed directly from the Internet, suitable for fast network. If you are not as fast as you are, or you want to save the downloaded installation file after installation, you should choose "Download Form Internet" Next time you no longer download and directly install it ", download and install the file (about 40 MB ).
In fact, the so-called "Local installation version" is also a package that someone else downloads all files from the Internet (applicable to China's national conditions)

Step select the installation Destination path and the path of the installation source file, and then enter the path where the installation package is selected.
Note that this is a major event. I did not see this step during the first installation. The GCC was not installed, and thus the file could not be compiled.
+ All default
+ Admin default
....
+ Devel default
+ Editors default
....
As shown in, you can double-click a node in the Treeview to change its status, such as default, install, uninstall, and reinstall. The default status is default. Many tools are not installed by default.
Here, I chose install on the default next to the all line and install it all to avoid future problems. (Note: The tree control here is different from the one below win. You can try to have a different response when you click "all" and "default" next to "all)

Step 4. Click next and the installation is successful. It automatically creates a shortcut on your desktop.

 

Now, let's start my Linux journey. Double-click cygwin to enter the system.
First, we will introduce several simple Linux commands.
PWD displays the current path
CD changes the current path. If no parameter exists, enter the home directory of the corresponding user.
Ls to list files in the current directory. This command has n multiple parameters, such as LS-Al
PS: list the current system process
Kill to kill a process
Create a directory using mkdir
Rmdir Delete directory
Rm delete file
Renamed the MV file or directory
Man online help
Less: displays the last few lines of a file.

Most Linux commands have many parameters and can be used in combination. Therefore, whenever you do not know or cannot remember which parameter to use, you can use man to find the switch. For example, if you want to find how to use ls, you can type
$ Man ls
The system Echo information is as follows:
Ls (1) FSF ls (1)
Name
Ls-list directory contents
Synopsis
Ls [Option]... [file]...
Description
List information about the files (the current directory
Default). Sort entries alphabetically if none of-cftusux
Nor -- sort.
-A, -- all
Do not hide entries starting.
-A, -- almost-all
Do not list implied. And ..
-B, -- escape
Print octal escapes for nongraphic characters
-- Block-size = size
Use size-byte Blocks
:
It's all about it, hey.

Let's write a hello World Program.
# Cd
The/home/Administrator directory is displayed. My current Logon account is administrator.

# Mkdir Source
Create a subdirectory called Source

# Cd Source
Go to/home/Administrator/source

# Vim hello. c

Start the vim editor to compile the program. Now it is necessary to briefly introduce vim.
In the Linux field, there are two most historical editors. One is VI, and the other is Emacs. VI has now evolved into vim, which is more powerful than the current VI. Vim and Emacs are two completely different things. Vim emphasizes using simple commands to complete functions, whether it is searching, replacing,
Regular Expression matching, compilation, linking, troubleshooting, inter-function jump, and so on are all completed in the command line, and it also integrates the direction keys on the four keys of hjkl. It can be said that, with vim, you can basically skip the mouse: P Emacs is a powerful tool to make a fuss about version control, simulate a variety of editing environments, and perform various operations on text, A powerful text processing system. Emacs is controlled by a combination of keys such as CTRL + ALT + K. Vim is controlled by simple commands such as Se (SET.
Because cygwin only provides Vim (Can I install Emacs myself? I haven't tried it yet), let's first try VIM: P
Vim loads text files in command mode and insert mode. The insert mode, as its name implies, is to input and edit text; the command mode is to input various Vim control commands, which are commonly used:
I enter the editing mode
H Shift left
J move down
K move up
L right shift
W disk storage
Q exit
! Emphasize execution

Some commands can be used in combination. If you modify a file and want to save the disk and exit, you can use WQ. If you want to discard the disk and exit directly, you can use Q! Now, let's try it out. Remember: In edit mode, return to command mode, press "ESC", and then press ":". In input command, switch from command mode to edit mode and use I, it must be hard to use Vim for the first time. Hey hey, I am used to it when I use Vim for the first time. It is really effort-saving.

Edit the hello. c file and enter:
# Include

Int main (void ){
Printf ("Hello world! ");
}
Then, enter the WQ command to return to the command line.
Enter the compilation command:
# GCC hello. C-O hello
After compilation is successful, you can check
# Ls
Look at hello.exe.
Okay. Let's run it.
#./Hello
What is the effect? Good, right?

Okey, now playing a C ++ Hello world,
# Vim world. cpp
Input:
# Include
Using namespace STD;

Void main (){
Cout <"Hello world! ";
}
Compile the C ++ program using G ++
# G ++ world. cpp-O world
Run it,
#./World

How can this problem be solved? All of you are experts. It is no longer a problem to compile some simple programs at this step: P
Okay. How can I use GDB to write makefile files.

 

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.