Compiling C and C++ programs using FireCMD and GCC

In this tutorial you will learn about the processes you need to go through in order to compile your C (or C++) programs. We are going to use the UNIX's popular gcc compiler. You will need to download it's Windows port i.e. MinGW (Minimalist GNU for Windows). You can download it's latest installer version by clicking here. While installing MinGW, make sure you tick both C Compiler and C++ Compiler options when it asks to select components.

Important: After installing MinGW, you will need to add it's bin directory path to the %PATH% environment variable. To do this you can right click on Computer (My Computer) icon and from Advanced tab click on "Environment Variables". Select PATH variable from the list and click on edit. Now you can append the MinGW's bin directory path at the end separated by a semicolon. For example, if you have installed MinGW in "C:\MinGW\" then your bin directory path will be "C:\MinGW\bin".

Creating the program (Editing source code)

You can edit the C or C plus plus program's source code using the FireTXT text editor. You can open FireTXT in new tab of FireCMD from New Tab sub-menu of the File menu. You can also use any other ordinary editor like Notepad.

Note that the filename must end with ".c" (for C program) or ".cpp" (for C++ program) extension, e.g. myprog.c or myprog.cpp. The program code must obey C or C++ syntax. Discussing the syntax is not in the scope of this tutorial but you can use the following hello world c++ program code for testing.

#include <iostream>
using namespace std;

int main()
{
 cout << "\nHello World.\nFireCMD shell rocks!" << endl;
 return 0;
}

You can copy the code given above, paste it in FireTXT editor and save it as "helloworld.cpp".

Compiling

Now we have the source code ready for compilation. If you don't have the latest version of FireCMD then you can download it from here.

Before giving command for compilation, you may need to change your current working directory in FireCMD shell to the directory location where "helloworld.cpp" or any other source code file that you want to compile exists. For example, if your c or cpp file resides in "C:\" drive then you can change your directory giving the command cd C:\. If you are already in the directory where your source code file resides then you don't have to give any command to change directory. You can check your current working directory using the pwd command. Note that it is not compulsory to change directory. You can avoid changing directory but then you will need to specify the complete(absolute) path to your c or cpp file while giving the commands given below to compile your program.

Just give the following command in FireCMD shell to compile your program:

C:\MinGW\bin\g++ -o helloworld.exe helloworld.cpp

If there are no errors in your code, FireCMD will return you to a new prompt without any error messages. This means that your code has been compiled successfully into a separate executable file named "helloworld.exe".

Note that we are specifying the complete path to gcc compiler i.e. "C:\MinGW\bin\g++". To avoid giving the complete path each time you want to compile a program, you can add the MinGW's bin directory to the path variable of FireCMD. To do this you can give this command: path -a. This will open Browse for Folder dialog box from where you will be able to select the directory. For example, if you have installed MinGW in "C:\MinGW\" then the path will be "C:\MinGW\bin\". After you have added the directory to the FireCMD's path environment variable, you can simply give the following command to compile.

g++ -o helloworld.exe helloworld.cpp

Note: If you have older version of FireCMD (Build 10 or older) then you will need to prepend the commands given above with keyword "cmd:". For example: "cmd: g++ -o helloworld.exe helloworld.cpp".

Running the program

You can run the executable program by simply typing in "helloworld" or "helloworld.exe" at the prompt as shown in the image below.

Compiling and running c++ program