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
g++ test.c
This compiles your code into an executable named a.out. To execute this type
./a.out
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;
}
You can compile this by typing
g++ test.cpp
and run it by typing ./a.out.
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
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/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
or, if you change the permissions so the file is executable using chmod u + x hello.py you can type
./hello.py
In this case, UNIX reads the "shebang line" #!/usr/bin/pyton a, finds the python interpreter and executes the pyton commands in the file. The file behaves like a free-standing executable.
Next, we make a simple web page.
Back to the index page