Saturday, August 21, 2010

C++ Compiler on Linux Ubuntu 9.10, version 2.0

Over here, I showed an easy way to compile and run C++ on Ubuntu 9.10. I have since slightly tweaked this method. Use the following steps:
Open your .bashrc file and add: PATH=”$HOME/bin:$PATH”
You’ll need to have GCC (type sudo apt-get install gcc in Terminal (Applications > Accessories > Terminal)).
Create a folder called “bin” in /home/YourUsernameHere/ , then open gedit (Applications > Accessories > gedit Text Editor).
Copy the following into a new document:
#!/bin/bash

#change to the directory where all files are saved
cd ./path/to/project_directory/

echo "Compiling $1..."

#compile project
g++ $1 -o compiled

#run it
./compiled

#keep running it while user wants to
echo "Do you want to continue? '0' if no, '1' if yes: "
read CONTINUE

while [  $CONTINUE -gt 0 ]; do
 ./compiled
 echo "Do you want to continue? '0' if no, '1' if yes: "
 read CONTINUE
done

#clean up
rm ./compiled

#completed
echo "Operation completed."

#and change back
cd
Save that code as “run” in home/YourUsernameHere/bin/. (Note: no file extension.) Don’t forget to change the cd command to point to the directory where your project C++ files are held.
Back in the terminal, type the following commands:
cd bin

chmod 0744 run
To use, simply use command “run filename.cpp”. The compiler will attempt to compile and run. If it compiles, the program will run; otherwise, if your program has errors, you’ll see the error reports. Don’t forget the .cpp at the end of the file name.
Oftentimes however, you might be interested in just seeing if a program will compile, and not interested in running it. So, in a new document in gedit, and copy and paste:
#!/bin/bash

#change to the directory where all files ares saved
cd ./path/to/project_directory/

echo "Compiling $1..."

#compile project
g++ $1 -o compiled

#clean up
rm ./compiled

#completed
echo "Operation completed."

#and change back
cd
Save that code as “compile” in home/YourUsernameHere/bin/. (Note: no file extension.) Don’t forget to change the cd command to point to the directory where your project C++ files are held.
Back in the terminal, type the following commands:
cd bin
chmod 0744 compile
To use, simply use command “compile filename.cpp”. The compiler will then attempt to compile the program. If you don’t see anything besides “Operation Completed”, the code compiled correctly. Otherwise, you will see the typical C++ errors.

No comments:

Post a Comment