Working with Microsoft Visual C/C++

General instructions for Setting up and using the C Compiler and the C++ Compiler
(inline assembly is available to both compilers - no special steps are necessary)

Microsoft Visual C/C++ IDE will manage a Workspace for you. A Workspace consists of 1 or more Projects. Projects consist of C files, CPP files, H files, etc. The reason for multiple Projects is you could write a DLL as one Project, and the EXE that uses the DLL as a second Project. Then debugging becomes much easier with both Projects under 1 Workspace. The SGI equivalent of a Workspace is a makefile (loosely speaking). MSVC manages a makefile (named *.mak). We will use the IDE and not get our hands dirty with the MSVC makefile (*.mak file).

Create a Project and Workspace

Adding Files to a Project

Diabling Micro$oft extensions (ANSI compliant)

Enabling exception handling

Changing compiler warning levels

Passing Command Line arguments

Opening an existing Project

Conditional Compilation (WIN32 vs UNIX)

Invoking the C++ compiler (without *.CPP naming)

Inline Assembly

Editing/Debugging Mode and Menus

Windows Programs vs DOS boxes

 

The quick and dirty way to create a Project and Workspace:

If you don't have a C or C++ file to start with:

If you already have a C or C++:

Create the Project and Workspace:

Before

After

Now, add any additional files your project requires:

Here's what an old CMSC 201 Project looks like:

Before

After

Now, Build All should complete successfully. If you look in the directory where your source resides, you'll find additional files used by the Microsoft Visual C/C++ IDE (Debug folder, a *.dsp, *.ncb, *.plg, and possibly a *.opt, and *.dsw file):

 

Disable Microsoft language extensions (write in ANSI C/C++):

 

Turn on exception handling:

 

Compiler warning level 3:

 

Passing Command Line Arguments:

 

How to open an existing Workspace:

 

Conditional Compilation

If you plan to target multiple platforms, take advantage of conditional compilation. Microsoft VC++ defines WIN32 for you during preprocessing, compile, and link. If using Irix 6.x, the following symbols may be defined: __sgi, __unix, __host_mips, _SYSTYPE_SVR4, __mips, _MIPSEB. Please visit sgi's tech pub page for details or see this page for a best guess. Any inclusions (or guarding) on a Window machine could be accomplished as follows:

#ifdef WIN32
    // Something platform specific
#endif

 

Inline Assembly

Generally speaking, to inline assembly in Visual C++, simply create an asm block as follows:

void Foo(void) {  // C/C++ Function Call
    __asm {
        // Inline here
    }
}

Two examples: Inline ASM from a C program and calling a C function in ASM. For more reading on techniques available, see Assembler (Inline) Tasks in MSDN (Microsoft Developer's Network) under the Visual C++ Documentation (sorry, no MAN pages):

 

How to invoke the C++ Compiler:

Microsoft's IDE does not differentiate between lower case and upper case to decide what compiler to use. So a *.C file is the same as a *.c file. The IDE will invoke the C compiler as a best guess. The two ways to invoke the C++ compiler:

How to set the /TP Switch:

And here's the Microsoft explaination of invoking the compilers:

Syntax

/Tcfilename
/Tpfilename
/TC
/TP

The /Tc option specifies that filename is a C source file, even if it doesn’t have a .C extension. The /Tp option specifies that filename is a C++ source file, even if it doesn’t have a .CPP or .CXX extension. A space between the option and filename is optional. Each option specifies one file; to specify additional files, repeat the option.

/TC and /TP are "global" variants of /Tc and /Tp. They specify to the compiler to treat all files named on the command line as C source files (/TC) or C++ source files (/TP), without regard to location on the command line in relation to the option. These global options can be overridden on a single file via /Tc or /Tp.

By default, CL assumes that files with the .C extension are C source files and files with the .CPP or the .CXX extension are C++ source files.

Examples

The following CL command line specifies that MAIN.C, TEST.PRG, and COLLATE.PRG are all C source files. CL will not recognize PRINT.PRG.

CL MAIN.C /TcTEST.PRG /TcCOLLATE.PRG PRINT.PRG

The following CL command line specifies that FOO1.C, FOO2.CXX, FOO3.HUH, and FOO4.O are compiled as C++ files, and FOO5.Z is compiled as a C file.

CL FOO1.C FOO2.CXX FOO3.HUH FOO4.O /Tc FOO5.Z /TP

 

Editing/Debugging Mode and Menus

When editting the program, the MSVC menu will have a Build Menu:

When debugging (running the program), the menu changes to the Debug Menu:

In general, once a workspace exists and your project is running, the three menus you will use are the File Menu, Project Menu, and Build/Debug Menus. The other menus are there to confuse a new user.

 

Windows Programs vs DOS boxes:

By default, MSVC++ will create an application that runs in a DOS box (a command line application). The code to create the command line app is handled by Windows, and is transparent to the programmer. Visual Studio will fork a child process and run your project. No special steps are necessary when creating a generic C/C++ program with main as an entry point. Use the Wizard included with MSVC++ if you'd like to create a Windowed application. In general, its difficult to turn a command line app into a Document\View windowed program (but not impossible).

Also note: do not stop the program by clicking the X in the upper right hand corner of the DOS box. Instead, use Stop Debugging from the Debug Menu (shown below) in Visual Studio (or use SHIFT F5 when MSVC (not the DOS box) has command focus). Win95/98 seems to get ill when a program is stopped using the DOS box.

Other useful commands are:

Last updated by Jeff, 5-16-2000