Pages

Thursday, 24 July 2014

Compile C, C++ Directly from command prompt

Today i'm gonna compile C and C++ codes directly through command prompt of windows operating system !! lets start :) 

First of all download  CODEBLOCKS IDE from here -- www.codeblocks.org  
Basically codeblocks is an Integrated Development Environment aka IDE
just install it on your system !!
then press the windows button and search for "Environment variable" and go for the second option that says "Edit the system environment variable" hit on that option !!
following window will pop-up on your screen --

now next thing you have to do is click on the environment variable :)
following window will pop-up --

Now under the system variable section scroll for variable "PATH" like this --
now double click on the path following edit system variable window will pop-up --
Click at the end of address bar in Variable value. now put there a semicolon (;)
here u have to add a address follow this following steps to paste the address

1.) Open your C drive where the files of your operating system are placed.
2.) Go into your program files look for "codeblocks" folder which we have earlier installed.
3.) Go into the codeblocks folder and then search for "MinGW" folder go into it.
4.) Go into the "BIN" folder thats it
5.)  See at the top at address bar and right click on the address bar and copy the address like this ..

Again come to the Edit variable system and paste the address after semicolon like this --
click ok and save the settings :)

Now open your text editor .. like Notepad(It Sucks, Please dont use it) , Notepad++(Awesome) or Sublime text(Awesome, recommended)

Type your C++ code Like this
Save it wherever u want(in this explanation i will save it on the desktop) and then open the command prompt
And then navigate to the directory with "cd" and "dir"
like this

now we have found our C++ code then we have to compile it to produce a executable file
for compilation of a C++ code use the following Commands
"g++ -o helloworld helloworld.cpp"
In case of C programming Use "gcc -o helloworld helloworld.c"
lets see what happens ..

 


You Can see in command prompt that a helloworld.exe file is created just type the name of the executable file i.e helloworld in command prompt and the file will be executed :) and your C++ or C source code is compiled as well as executed like this

Thanks for giving your valuable time on reading this post :)  Happy Programming!!

No comments:

Post a Comment