COSC1337 | C++

ACC

C++ at the Command Line

Foreword

Almost all C++ development is performed using a sophisticated IDE such as Visual Studio. However, it is interesting to see a simple example of C++ run from the command line.

Use a Simple Editor

Construct a code example like the following using a simple text editor like Notepad++.

 

Open the Developer Command Prompt for VS

If you have Visual Studio installed and have chosen the path options then your system should have the Developer Command Prompt for VS installed. Open the Developer Command Prompt for VS by typing devel... in the 'Type here to search' box.

Be sure to R-click and select Run as Administrator

 

For convenient reaccess, pin the program to the taskbar by R-clicking the icon on the taskbar and selecing 'Pin to taskbar'.

 

After pinning to the taskbar, R-click the icon, R-click Developer Command Prompt for VS... and then select Properties.

 

Select Shortcut in the Properties dialog box and then Advanced... Select Run as administrator | OK | OK.

 

Display Source Code in the Command Window

To make the Developer Command Prompt visually identifiable, I changed the background color from black to white and the font from white to blue. Display the code written above with 'type source.cpp'.

 

Compile and Run the Source File

If Visual Studio is installed on your system, you should be able to run the compiler from the command line. cl invokes the compiler. /EHsc sets exception handling to synchronous mode.

Display the directory contents with the dir command. Notice the new source.exe and source.obj files. source.exe is the program executable file. source.obj contains pre-linked compiled code.

Run the program by typing the program name at the command line. Notice that the .exe extension is not required.