Unix Fundamentals: Compiling




Compiling programs at the UNIX command line is simple and straightforward. We will go over the particulars for C, C++ and Java.

C and C++ Create a file called test.c in one of your directories. Enter in the following code:

#include <stdio.h>

int main(void)
{
puts("Here is my test program");
return 0;
}

Having done this, write and quit. At the UNIX command line enter

$ make test

This compiles your code into an executable named test. To execute this type

$ ./test
Here is my test program

In our configuration of the UNIX system, all executables must be preceded by ./

Here is a sample C++ program; give it the name test.cpp

#include <iostream>
using namespace std;

int main(void)
{
cout << "Here is my test program" << endl;
return 0;
}
$ make test

This compiles your code into an executable named test. To execute this type

$ ./test
Here is my test program

Java Here is a sample program in Java. Create this file in vi and name it Foo.java

public class Foo
{
public static void main(String[] args)
{
System.out.println("Test Java Program");
}
}//end class Foo

To compile this program enter the command

$ javac Foo.java

To run the program enter

$ java Foo
Test Java Program

The program you run java on must have a main in it!

Python Begin by creating a Python program in vi; here is a very simple one. Name this file hello.py

#!/usr/bin/env python
print ("Hello, World.")

Create this file, save it and return to the command line. To run it you may do either of the following

$ python hello.py
Hello, World.

or, if you change the permissions so the file is executable using chmod u + x hello.py you can type

$ ./hello.py
Hello, World.

In this case, UNIX reads the "shebang line" #!/usr/bin/env python , finds the python interpreter and executes the python commands in the file. The file behaves like a free-standing executable.

Next, we make a simple web page.