Saturday, June 02, 2012

Compiling a C++ (.cpp) program on the command line of Windows 7 (cmd.exe).


If you are new to this matter like me, and you don't want to use the crap that Microsoft Visual Studio has to offer then follow these simple steps, and you'll be able to write and compile your own programs using notepad or, I strongly recommend using, notepad++. I am writing this because this steps worked well for me in my Windows 7 machine, and I want to keep a record of what I did today and maybe someone one day might read this and find this information useful. 


1.- Download and install MinGw I, downloaded the automated version which is basically an already compiled ready to use .exe (If you like it hard, then you can download the files and compile them your self) from this web page: http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/mingw-get-inst-20120426/ (maybe a new version has been released you I encourage you to check that out).


2.- Download the file and follow the installation instructions, they are not hard to follow. I did the installation of the files directly in my C:\ drive to save some kind of confusion latter on. 


Now you need to specify the path where g++ (which is the compiler I'm going to use) has been installed to the command line (cmd.exe).

3.- Open cmd.exe and type the following and then hit ENTER. 
set PATH=%PATH%;C:\MinGw\bin 
Remember I am using C:\MinGw\bin because that is the location where I installed MinGW, you might have use other one. 


Now you can test if this works by writing a simple C++ program on your notepad.exe and save it as NAME.cpp, in your Documents folder it may be something like:


------------
# include
using namespace std;


int main()
{
cout << "this is stupid";
return 0;
}
------------


Now on the command line (cmd.exe) go to your Documents folder (cd C:\Users\victor\Documents) and see that your file NAME.cpp is actually there by typing DIR (which will show a list of all the files and folders that exist in the Documents folder). 


Now the exiting part: 


COMPILE your file using the following instructions: 


g++.exe "NAME.cpp" -o "NAME.exe"


you will see that an exe file has been created called NAME 
and if you write NAME.exe on the command line it will say "this is stupid"


Well that was it. I hope this has been useful to you as it was for me... 


Regards. 



No comments: