Starter 2: Useful tool for fast C++ programming practice under Linux


1. Introduction
2. SciTE
3. g++ compiler
4. HelloWorld.cpp
5. Compile and Run the program




1. Introduction

Last post we talked about the tools Notepad++ and C++ Builder and CMD for programming C++ under Windows system, it is a fast and easy way for starters to practice C++. However, once you have encountered Linux/Unix system, you will find how could this system so beautiful and amazing, especially for our programmers. In this post we try to make an easy way for the programmer rookies even Linux rookies, to start their "Hello World.cpp" under Linux. Let's have a try!

2. SciTE

So, as we use Notepad++ in Windows, a similar test editor(source code editor) called SciTE is that one under Linux system, and also it is FREE! (Barely seen the non-free software under linux, right?). 

Now days, relative newer versions of Ubuntu(one of the famous Linux operating systems) has an easy way to install software named "Ubuntu software Center", instead of the "sudo apt-get install ..." command, this visible, windows-like interface allows you search and install software just like your in windows system. 



Installation. 
My Ubuntu is 10.04LTS, just click "Applications" in the menu bar above the desktop, and find the "Ubuntu Software Center", search the "SciTE" in search bar and click "install". Well done! Then you can find and open the SciTE editor from the "Applications"-->"Programming"-->"SciTE Text Editor".






3. g++ Compiler


g++ is one of the GNU compilers, which is totally FREE and widely used in Linux systems. There is another c/c++ compiler gcc, which is also commonly used in Linux and for most cases it is same as g++. So choose one you like and don't care more about it.

g++/gcc is usually installed once you finished your installation of Linux system. So, you DON'T have to install the compilers on your own. An easy way to check the version of g++/gcc is to use command "--version". Go  into the terminal, type command "g++ --version" or "gcc --version", you can see the version information.





4. HelloWorld.cpp

We still use the code from the last post. Save it anywhere you like, and give a name with extension .cpp to make it a C++ file. 


#include <iostream>

int main(){
 using namespace std;
 char ch;

 cout << "Type, and I shall repeat.\n";
 cin.get(ch);

 while (ch != '.')
   {
     if (ch == '\n')
       cout << ch; // done if newline
     else
       cout << ch;
 
     cin.get(ch);
   }
 return 0;
}
  


5. Compile and Run the program

There are two ways to compile the program. One is using the SciTE eidtor and one use terminal command.

To compile the program in SciTE, just click "Tools"-->"Compile". There will be a command window showed. If there is no error, you can run the program. Because the input stream is ignored in the command window, where only output stream can be showed, you can only run the program in the terminal if your program needs input. (I don't know why, but it happens only under Linux system)



To compile the program in terminal, open terminal, go into the folder where you save your cpp file. Type "g++ HelloWorld.cpp -o HelloWorld" to finish the compiling. If no error occurs, type "./HelloWorld" to run the program. 






No comments:

Post a Comment