Tuesday, November 17, 2009

Writing your first program - Hello World


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

Enough words. Now it is time for some code. We are going to write our first program. Fire up your favorite text editor. We will write code in this simple editor till our work grows to several files. If you already have some IDE for the intended language, you can use them as well. Here we go-
  • C++
  • Java
  • C#
#include
using namespace std;

int main()
{
cout << "Hello World!\n"; }

namespace HelloWorldApp
{
public class HelloWorld
{
    public static void Main()
    {
        System.Console.WriteLine("Hello World!");
    }
}
}

Each program, when compiled, specifies a special function as the Entry Point. When a program starts, the platform looks for this function and all other functionalities must be reachable from this point. C++, Java and C# differ how they specify the entry point function, even though the standard entry point function is named main1 in all these languages.
  • C++
  • Java
  • C#
int main(void);
int main(int argc, char *argv[]);
public static void main(String[] args)
public static void main(String... args)
static void Main();
static void Main(string[] args);
static int Main();
static int Main(string[] args);
1. You can specify other function as Entry Point though this is very rare.

Compilation:
I have already discussed various compilers and IDEs for C++/Java/C# in the previous post. Now its time to compile and run our first program.
  • C++
  • Java
  • C#
Windows:
cl.exe helloworld.cpp
Run the program
helloworld.exe

Linux (GCC): There are 3 variants to compile a C++ program with gcc -
  1. gcc helloworld.cpp -lstdc++ -o helloworld
    gcc is usually used for C compilation.
    -lstdc++: compile in C++ mode.
    -o: indicates the output filename. If omitted, output filename is always a.out
  2. g++ helloworld.cpp -o helloworld
    uses gcc to compile in C++ mode.
  3. c++ helloworld.cpp -o helloworld
    Most systems install this program which is identical to g++.
Run the program
./helloworld
Sun Java:

javac HelloWorld

GCC (very rare):

gcj --main=HelloWorld HelloWorldApp.java -o HelloWorldApp

Run the program (Remember Java as well as Linux-File-System is case sensitive)
java HelloWorld
Windows (MS SDK):
csc.exe HelloWorld.cs
Run the program HelloWorld.exe

Linux(Mono):
gmcs HelloWorld.cs
Run the program ./HelloWorld.exe


Source Code for the Posts:
I have hosted a project in google code. The project page is cpp-java-csharp. Click the source tab. You can browse the source code by clicking browse tab or can download to your machine by clicking Downloads tab.

Structure of the Source Code:

As discussed in the previous post, I will use the following compilers and IDEs for our exercise works-

C++
  1. Windows:
    Visual C++ 2008 Express Edition
    Just open the solution file on Visual C++ 2008 Expression Edition or Visual Studio 2008. Compile and run.

    Command Line
    A batch (.bat) file will accompany each project. You need to run this batch file which will generate the executable .exe file. I assume that you already have the C++ compiler (cl.exe) in the path.
  2. Linux:
    Command Line
    A shell script (.sh) will accompany each project which will generate the executable file. The gcc must be in the path. Change the mode of the .sh file to executable with the following command:

    sudo chmod +x xyz.sh

    Run the file with the following command if you are in the project directory:
    ./xx.sh

Java
Eclipse:
  1. First create a new Java project in your workspace.
  2. Click "File->Import..." menu item.
  3. Import the project from the downloaded package
Netbeans:
Open the project from the downloaded package with "File->Open Project" menu item.


C#
Windows:
Visual C# 2008 Express Edition
Same as C++

Command Line
Same as C++

Linux:
MonoDevelop
Open the monodevelop project in the IDE. Compile and Run.

Command Line
A shell script (.sh) will be provided with the project. Consult C++ section to know how to run shell script in console.

Monday, November 16, 2009

Setup development environment


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

Fasten your seat belt. Now its time for something practical. We will start with setting up the environment for software development in C++, Java, and C# languages. which will be followed by our first program "Hello World". The next posts will cover the various syntactical differences of these languages. On the journey, we will gradually develop the hex viewer that I promised to program as part of these blog posts. I will also briefly explain various tools, techniques, libraries, and technologies for the cutting edge software engineering at appropriate places.

There are numerous compilers available for each of these languages. Depending on operating systems and distributions, the installation process of these compilers varies enormously. You should consult your OS documentation to find an appropriate compiler. Most of these compilers are accompanied by feature-rich integrated development environment (IDE) that usually consists of a text editor with syntax highlighting for the source code and a collection of tools accessible directly from this text editor. The most common tools are - compiler for obvious reasons, debuggerto check programmatic errors, management of sources files, numerous wizards for target-oriented automatic code generation, automatic unit testing etc.

We will see here the most widely used IDEs and compilers on Windows and Linux. Some of them are available for both operating systems and/or can generate cross platform binaries. I have listed them under cross-platform tag.

  • Windows
  • Linux
  • Cross-Platform
Microsoft Windows SDK: Microsoft provides a comprehensive set of compilers for numerous programming languages as part of Microsoft Windows SDK. The C++ compiler that it supports is called Microsoft Visual C++ (MSVC), which was aimed to visually write C++ code similar to Visual Basic. The newest version is highly compatible with C++98 standard and also supports most of C++0x, the upcoming C++ standard. MSVC can compile both in C and C++ mode. The C compiler supports the original C89 standard along with some features of C99. It includes a lot of Microsoft-specific functions. So special care must be taken for cross platform programs. The MS-specific functions might not compile in other compilers.

Windows SDK includes a C# compiler called csc. Microsoft is the inventor of C#. So the compiler that is shipped with Windows SDK is the most standardized and latest one. The C# 3.0 is released with the .Net Framework 3.51. The upcoming .Net Framework 4 will include C#4.0.

Microsoft has provided a Java compiler named Visual J++ for many years. There were lots of legal tussles over Microsoft's use of Sun Java technology till the 2001 settlement. Since the introduction of .Net technology, MS has transformed its J++ transparently into Visual J# which supports writing Java source code to build applications and services on the .NET Framework. As of January 10, 2007 Microsoft has discontinued visual J# offering. So I would rather not use J# for future Java development.

The windows SDK is free to download and you can use your favorite ASCII or Unicode text editor (e.g. Notepad) to write programs and compile them from command line. Prior to Windows Vista, the SDK was called Platform SDK. The latest SDK can be downloaded from Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1. Follow the instructions that accompanied the downloaded package to setup the programming environment.

C++
Open a command prompt (Start -> Accessories -> Command Prompt) and type the following:

cl  /help

It will show you various command line options that are available for C++ compiling.
Java
Write following command from command prompt:

vjc  /help

It should show you the options for J# compiling. Remember J# will compile your Java code to MSIL for .Net runtime, not Byte code for Java Runtime.
C#
Start the C# compiler from command prompt:

csc  /help

The options for C# compiler will be displayed.


Microsoft Visual Studio: Microsoft Visual Studio is undoubtedly the most popular IDE in the world. Its current version (VS2008) and upcoming version (VS2010) have a powerful set of tools for application development. There are hundreds of plug-ins available both freely and commercially to enhance it capabilities even further.

You need something useful from Microsoft, you have to pay for that. Luckily, a stripped down version of visual studio is made available for the hobby programmers. It is called Visual Studio Express Edition. You can do a lot of programming with it. There are separate versions of express editions for each of Visual C++, Visual Basic, and Visual C#. The expression edition is available at http://www.microsoft.com/express/download/ for download. Download and install your preferred language version.

SharpDevelop is another IDE written in C# for C# compilation. It is open source and can be downloaded from http://www.icsharpcode.net/OpenSource/SD/. Last time I used it, it was incredibly slow even for a moderate size project.
Unless you are a comnand line guru, you need some IDE. Buy Visual Studio 2008, if you can. Otherwise, download express edition or some free alternatives. Also see cross platform tab.
GCC:
GNU compiler collection (GCC) is the most favorite C/C++ compiler among the open source programmers. It laid the foundation for the free software movement during the 90s and still the dominating compiler collections in the Linux operating system. Even though it is free, it provides a comprehensive set of tools and extremely flexible compilers for cross platform application development.

If you are using a Linux it is very usual that you already have GCC installed. Otherwise, you can install preconfigured and prepackaged version for your distribution using the package manager of your system. I am a Ubuntu user. For me the commands are:

sudo apt-get install gcc
sudo apt-get install g++

If you are a Linux geek and want build your own version, download the latest version of GCC from ftp://ftp.gnu.org/gnu/gcc/. You have to go through the usual steps of software building in Linux system: Configure -> Build -> Install. Enjoy !!!.

GCC also includes a compiler for Java called GCJ. It was drawing attention when sun java was not yet open source. But now-a-days, most distributions prefer sun java over gcj.

Now check whether your installation is working properly. Open up a terminal and run the following commands-
C++
The command for C++ compiler is g++. Run in the terminal -

g++   --version

You should see the version information for g++.
Java
If you want to use the java compiler of GCC, run the following command in a terminal -

gcj   --version

It should show you the version information of the gnu Java compiler.

For Java and C# on Linux, see cross-platform tab.
Many *nix enthusiast use generic editor like emacs and vim as IDE. For popular IDEs on Linux see cross-platform tab.
Virtualization:

It is the process of running one operating system on top of another. You can run Linux on your windows machine or vice versa with appropriate virtualization software. VMWare is the market leader providing virtualization software for all popular OS. VirtualPC is another virtualization software from MS, but it supports only one version of Windows to run on another. Sun's VirtualBox is also a popular alternative. It has a open source version called VirtualBox OSE which can be freely downloaded and installed.

GCC:
GCC is originally intended for the *nix based program compilation. But there exist various ways to use it on Windows OS. I have listed some of options available to run GCC in Windows OS:

Emulation:Cygwin (Gnu + Cygnus + Windows) is a Linux API layer providing Linux functionality. You can install cygwin in your windows machine to easily use your familiar linux commands in windows. I have used this emulation layer for quite a long time in my office PC where Linux installation was strictly forbidden. You can compile your programms with GCC under cygwin same way as you do it in Linux.

Software Port:MinGW (Minimalist GNU for Windows) is a port of GCC compiler to build applications for Windows. It provides a comprehensive set of libraries for native windows program development.

Java Development Kit (JDK):

You need JDK to write and compile Java programs. In Windows, you can decide to use J# for Java code in which case your program will be a .Net program and run only on .Net runtime. Download the latest JDK from Sun Java Download Site and install it in your system. If you are a Linux user, you can decide to install the prepackaged version from your distribution. Ubuntu/Kubuntu user can look at Installing Sun JDK 6 in Ubuntu/Kubuntu Interpid.

Eclipse:

Eclipse is the most favorite IDE for Java based application development. It has a plug-in based architecture. Eclipse defines only some core functionalities by default which are enhanced by plug-ins for the intended development environment. The eclipse web site already provides prepackaged plug-ins for various programming languages. For cross-programming environment you just need to install the required packages from its Help->Software Updates... menu command.

The installation of Eclipse is the simplest of all. For Windows OS, download your intended package from the above link, uncompress it, hurrah! you are finished. Now run <directory_for_uncompress>\eclipse.exe. The procedure is also same for the Linux. Just in case, you need the commands for uncompressing look at Most Useful Linux Commands. If you are using some popular distribution, you can also use the respective package manager so that your the installation and uninstallation can be manipulated by the package manager front-end. In my kubuntu system, the command is-

sudo apt-get install eclipse

Just remember, your distribution might not provide the latest version of eclipse at the time of installation. For more information regarding installation of eclipse and Java in Ubuntu/Kubuntu Interpid, look my old blog post at "Set up Java Development Environment in Ubuntu/Kubuntu Interpid".

If you want to install other development environment, click "Help -> Software Updates..." menu command and choose the appropriate programming language like in the following figure.



Netbeans IDE:

Another very famous cross-platform development environment is Netbeans. It has many cool features which made it an attractive Java IDE. There are also plug-ins for other programming languages like C++. It is equipped with a huge set of wizards for generating codes for frequently needed projects and code modules. Download the latest version from http://netbeans.org/downloads/.

Remember for C/C++ development with Eclipse and Netbeans IDE, you have to provide a C++ compiler to them. On Windows, you can use Microsoft C++ compiler or Intel C++ compiler. You can even use GCC over MinGW or CygWin described earlier.

Mono and MonoDevelop

So you love .Net for its cross-language feature or C# for its powerful but simple syntax. But you are a Linux user or your program should run both on Windows and Linux. This can frequently happen if you develop ASP.NET program and want your web application to be hosted both on Apache and IIS. You need Mono. This is an open source implementation of Microsoft's .Net Framework based on the ECMA standards for C# and the Common Language Runtime for Windows, Linux, and Mac OS X. It currently supports .Net Framework 2.0, compiler for C# 1.0, 2.0, and many of 3.0 features. MonoDevelop

Mono framework is complemented by a cross-platform IDE which runs on Windows, Linux and Mac OS X. It is a full-featured IDE and you can develop .Net programs which run on all these platforms.

Windows installation of Mono and MonoDevelop are provided through MSI packages. Download them and click. You should be provided with usual Windows Installation procedures. Many linux distributions are currently providing prepackaged Mono and MonoDevelop. Follow your package installation procedure to install and configure them. Installing Mono & MonoDevelop is simpler to install in Ubuntu than in Windows once you have an active Internet connection.

1. Open console
2. sudo apt-get install mono-runtime
3. sudo apt-get install monodevelop

Use either Eclipse or Netbeans for Java and C++. MonoDevelop for C#.


For beginner, I can only recommend the following from my personal preferences:
C++
Windows:
Visual C++ 2008 Express Edition

Linux:
Eclipse for C++
Java
Eclipse or Netbeans
C#
Windows:
Visual C# 2008 Express Edition

Linux:
MonoDevelop