Tag Archives: c

Compiling a C Program from Visual C++ 2015 Command Prompt

For quick compiling of C/C++ programs on a Windows PC,  the Visual C++ 2015 Command Prompt is your go to program. Many programs have no need for development in an IDE.

Before you can compile a c/c++ program you will need to install the Microsoft Visual C++ Build Tools 2015.

Once installed you will have a new list of shortcuts in your Start Menu.

Visual C++ Build Tools Start Menu Shortcuts
Visual C++ Build Tools Start Menu Shortcuts

If you right click on any one of the Visual C++ 2015 Programs and choose Open File Location you will also see the extensive list of tools.

Visual C++ Build Tools Explorer Shortcuts
Visual C++ Build Tools Explorer Shortcuts

Depending on what system architecture you are running, open one of the programs. If in doubt select:
Visual C++ 2015 x86 Native Build Tools  Command Prompt
or
Visual C++ 2015 x64  x86 cross Build Tools  Command Prompt

The following window will appear:

Visual C++ 2015 Build Tools Command Prompt
Visual C++ 2015 Build Tools Command Prompt

To verify that the prompt is functioning property, type cl and the prompt will output the exact same information again.
Note: You can use the Visual C++ 2015 Build Tools Command Prompt the same way you would use the standard command prompt for directory navigation.

  1. To test a simple C program, first create a test directory to hold your test program. md c:\test to create a directory, and then enter cd c:\test to change to that directory. This is where your source files and executable will be stored.
  2. Type notepad test.c  When the  Notepad alert dialog  pops up, choose Yes to create the new test.c file in your current working directory.
  3. In Notepad, enter the following code and save as test.c:
#include <stdio.h>
int main()
{
printf("Hello World!\n");
getchar(); //used to prevent executable from closing when double clicked
return ;
}
  1. Now from the Visual C++ Build Tools Command Prompt type: cl test.c. If the program compiled successfully you will see: /out:test.exe and test.obj. In your test folder you will now have the test.c source file along with test.obj and test.exe
  2. To run your newly compiled program simply type test and your program will run in the command prompt. You can also double click the test.exe executable.

To compile a program with more than one source file simply type:

cl test.c test2.c test3.c

The compiler will output a single file called file test.c
To change the name of the output program, add an /out linker option:

cl test.c test2.c test3.c /link /out:mynewprogram.exe

test.c source file