Tuesday, June 25, 2013

Compile and Run C Program using Visual Studio 2012 Express

You can compile and run a C program using Microsoft Visual Studio 2012 Express editions. This procedure has been tested using Visual Studio Express for Desktop running on Windows 7. Windows 8 users may try this procedure provided you install Visual Studio Express for Desktop.

There are two methods to run and compile a C program. The first method is using command line and the second method is using Visual Studio IDE.

Compile C Program in Command Line using VS Express


1. Select Start >> All Programs >> Microsoft Visual Studio 2012 >> Visual Studio Tools >> Developer Command Prompt for VS2012. See below.



2. Launch the "Developer Command Prompt for VS2012".

3. Navigate to the location where you wrote the program.

4. Compile the program using the command
cl <program_name.c>

5. The obj file and exe file will be created using the same program name.

Example:

I've wrote this simple program using notepad and saved into my document folder under cprogram1.c

Important: Please make sure that you save the file in program.c instead of program.c.txt. Notepad save everything in .txt extension by default. To avoid this problem, under the file explorer select Organize >> Folder and search option, select the view tab and clear the check on "Hide  extensions for known file types".

#include <stdio.h>

int main ( )
{
     printf ("This is a basic C program. \n");
     return 0;
}

I've launch the command prompt as follows


I run the program as follows


You cal also refer to this page from MSDN on compiling C program using command line.

Compile C Program in VS Express 2012 IDE


Step 1.
Launch Microsoft Visual Studio 2012 Express >> VS Express for Desktop.


Close the start page.

Step 2.
Select FILE >> New Project


You will see the dialog box as below


Select C++ and highlight the option Win32 Console Application. Change the name of the project if you prefer.


Click "OK".

Click "Next >"


Make sure that Console Application is selected. Clear all other options and checked "Empty project". As shown below:


Click "Finish"

The project screen is shown below:


Step 3
On the right hand side, under "Solution Explorer", right click "Source Files". Select Add >> New Item.


The dialog box is shown below


Change the file name from Source.cpp to any name with extension .c. In this example, I changed to Sample1Main.c



Step 4
Type your C program on the left pane.


Click "Save"

Step 5.
To compile the program click BUILD >> Build Solution


The output screen will show the following results


Please be reminded that the exe file is located under Projects\projectname\Debug\projectname.exe

Step 6.
To run the program you must open a command prompt and navigate to the file location since this is a console application.


Please note that besides the exe file, there is also a ilk file and pdb file.

Ilk file is Linker Input File, you can find explanation here.
Pdb file is Program Database file, you can find explanation here.

Additional Note: 
The disadvantage of using the IDE is that many files are created unnecessary. However, the advantage is that you can use the IDE for auto-complete and error detection.

Besides VS 2012 Express. You may try Eclipse or NetBeans since both IDE also support C program. Alternatively, you can download programmer's text editor such as Notepad++ for program editing and use the command line for compilation.

*****  

9 comments:

  1. I just moved the app on another machine and stopped working ? Is asking for msv* dlls?
    This is very frustrating. On Linux runs on any distro and version ? What's going on Bil ?

    ReplyDelete
    Replies
    1. You might need to download Microsoft Visual C++ 2012 Redistributable Package. Good Luck.

      Delete
  2. I don't even know....
    I tried this and got some asian characters instead of the expected english text in the most basic win32 API example. Gonna try a different IDE I guess :D

    ReplyDelete
  3. when i use the cl command it says
    'cl' is not recognized as an internal or external command,operable program or batch file

    ReplyDelete
    Replies
    1. This means that Visual Studio 2012 did not installed properly or the path of cl did not registered.

      First, locate the cl.exe. It should be under program files/Microsoft Visual studio 2012. Alternatively, do a search for cl.exe. If you can't find cl.exe reinstall visual studio.

      After you have located the file cl.exe , take note of the path location of the file. If you navigate to the location where cl.exe is, you should be able to compile the program.

      To allow yourself to run cl from anywhere, you must make sure that the environment variable PATH includes the location of cl.exe. Use the command "path" to find out if the current path includes cl.exe. To edit path, you can right click My Computer, select advanced system settings, select advanced and click Environment variables. Locate the path variable and click edit to add additional path.

      Delete
  4. how to generate .hex file in visual studio

    ReplyDelete

Note: Only a member of this blog may post a comment.