Wednesday, August 22, 2012

An Ubuntu Bash Script to Compile a C++ Program

After I have installed the C++ and WxWidgets 2.8.12 on Ubuntu 12.04 LTS, I was having a lot of problems in compiling the sample codes given in the tutorial.

Finally, I found this command:
g++ `wx-config --cxxflags` -o OutputFileName SourceCodeFileName.cpp `wx-config --libs`

But, if every time to compile your source code file, you need to remember this command may be a bit troublesome.

So, in order to quickly compile the cpp file, I have created a simple bash script file (like a DOS batch file in Windows), which allow you to just type:

$ ./ccpp.sh hw1

* Note, this is assuming your source code file is hw1.cpp and your output file is hw1. You should create an empty directory, and it should just contain hw1.cpp file. So, after you run this script file compilation, you should see two files: hw1.cpp and hw1 (hw1 is the output file which can be run directly by typing ./hw1)


And the output will be a file hw1 which is already executable (this is assuming that there is no bugs in your code).

So, to create the bash script file, create an empty text file, and then copy the following codes into it:

#!/bin/sh
#
# This script will take one argument, the file name. For example, if you have a file called hw1.cpp and this script will compile the hw1.cpp and output # a file called hw1. To execute this script, at the terminal type:
#
# ./ccpp.sh hw1
#
clear

g++ `wx-config --cxxflags` -o $1 $1.cpp `wx-config --libs`

echo ""

echo ""

After you have typed above, save it as "ccpp.sh". Then open a terminal, and type the following at the command prompt:

$ chmod +x ccpp.sh

Then it is done. Enjoy the wxWidgets tutorial :)

Converting a Physical Linux to Virtual

Hmm ... I have done a lot of work on my Linux Lubuntu 15.10 with PHP and PostgreSQL and a few other things ... it is quite time-consuming to...