Compiling And Running Programs

From CSWiki
Jump to: navigation, search

Compiling and running programs in the lab is very easy. Below you can find instructions on how to do just that in various languages. Please feel free to add your own!

[edit] A Note on Compiling Programs

Some people like to append .exe to the end of executable programs when they are compiling code. This is entirely unnecessary and generally looked down upon in the Linux world (because it is an artificial restriction imposed on Microsoft Windows). The normal thing to do is to use the name of the program with no extension. However, if your professor demands one way or the other - listen to them!

[edit] A Note on Running Programs

Lab machines can be set up to allow you to run programs without the leading ./ before the program name. This can make life easier for students, however it can be seen as a security risk. If someone has placed a malicious executable file with the same name as a system command in your current directory, and your executable path order is changed then when typing the system command (such as ls or gcc), it could run the malicious executable, possibly erasing all your files.

If you absolutely want this functionality after understanding the risks then you can add the local directory to your path on the lab machines using:

$ export PATH=$PATH:.

This behavior can be saved by putting the above command into ~/.bash_profile (assuming you are using bash as your shell).

[edit] C Programs

Programs written in the C programming language (usually ending in .c) can be compiled and run as follows,

$ gcc program.c -o program
$ ./program

You may also want to take a look at Introduction to Makefiles.

A simple C program looks like this:

#!cplusplus
#include <stdio.h>
int main()
{
    printf("Hello, world!\n");
    return 0;
}

[edit] C++ Programs

Programs written in the C++ programming language (usually ending in .cpp or .cxx) can be compiled and run as follows,

$ g++ program.cpp -o program
$ ./program

You may also want to take a look at Introduction to Makefiles.

A simple C++ program looks like this:

#!cplusplus
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}

[edit] Python Programs

Python is a dynamic, object oriented, byte-code compiled, interpreted language, so you don't compile anything. In order to run a Python program you must either invoke python manually or make your script executable (and include #!/usr/bin/env python on the first line of the file)!

[edit] Make the Script Executable

You need only issue this command once.

$ chmod +x program.py

[edit] Run the Program

$ ./program.py

A simple Python program looks like this,

#!python
#!/usr/bin/env python
print "Hello, world!"

See Learning Python for more information.

[edit] Ruby Programs

Ruby is a dynamic, truly object oriented, interpreted language, so you don't compile anything. In order to run a Ruby program you must either invoke ruby manually or make your script executable (and include #!/usr/bin/env ruby on the first line of the file)!

[edit] Make the Script Executable

You need only issue this command once.

$ chmod +x program.rb

[edit] Run the Program

$ ./program.rb

A simple Ruby program looks like this,

#!/usr/bin/env ruby

puts "Hello, world!"

Personal tools