Zack's Website

My Egotistic and Esoteric Website

Compiling on Linux

How to Compile a Program on Linux

I'm still a fan of compiled languages.  There's been a growing trend and push toward interpreted and just-in-time compilation, and even dynamic languages.  They all have their place, but there's just something I still enjoy with compiling a program, checking for type safety, etc.  I don't call you a programmer unless at some time you've had to develop something in C or C++, and even use a debugger to trace through memory.  You've just got to do it to know what's happening under the covers of a program and inside the computer.  I can't tell you that I can move DWORDS into registers, and shift binary bits to perform addition, etc anymore, but understanding assembly language and some low-level development is a necessity of any good programmer.  That's just my $0.02!

Anyway, here's a simple ANSI compliant C++ program...

#include <iostream>
using namespace std;

int main(void)
  cout<< "This is a test of the emergency broadcast system.\n";
  return 0;

Simply take that and stick it into your favorite text editor and save it as "test.cpp".  Then from a command line type...

g++ test.cpp -o test

It will compile without a hitch (as long as you're using the GNU compiler on a Linux/Unix machine - in this example), then type "./test" to see it print the message.

What does the above program do (in pseudocode)?

First, it instructs the pre-compiler to load the input/output stream header file so the compiler can learn about input and output functions like "cout" (which is a function to write out text to the standard output stream.

Then it tells the compiler, that any functions that it sees should come from the "std" (standard) namespace.  Which just means that if developer writes a function called "cout", it should look at the standard C++ libraries first, then anything after that.  It's an ANSI standard thing (back when I did C++ development we didn't have to include namespace declarations).  Circa 1991 (if you were interested to know when I did C development.)

Thirdly, it tells the compiler, to start at a function "main" that takes no parameters (void) and return an integer denoting its success or failure.  Here "0" means no "error".

Lastly, you'll see the meat of the program which is the writing of the line "This is a test of the emergency broadcast system." and a new-line constant.  Then returns 0; and we are done.

Pretty simple, eh?


Want to Try to Compile a C# (via the Mono Framework on Linux)?

using System;
namespace Test
  public class Test
     static public void Main()
       Console.WriteLine("This is a test of the emergency broadcast system.\n");


Simply take that and stick it into your favorite text editor and save it as "test.cs".  Then from a command line type (assuming you have the Mono Framework installed)...

mcs test.cs

Then type "mono test.exe" to run the application.  

"Dude, they look pretty much the same.", and I'd say you are correct.  But the MAJOR difference between a C++ and C# program is that a C++ program gets compiled to machine code, whereas a C# program gets compiled to an IL (Intermediate Language).  Essentially, it's a "virtual" machine language.  Then in C# the .Net Framework JIT's (Just-In-Time) compiles the code and executes it.  Whereas, a C++ compiled/linked program is ready to run on the type of machine it was compiled for (32-bit i386 for example).  Also the .Net Framework handles memory allocation/deallocation, garbage collection, etc.