This documentation is archived and is not being maintained.

How to: Compile a Code Example from the Help Topics 

Visual Studio 2005

The documentation for Visual Studio 2005 includes code examples that demonstrate the types of programs you can create. You can view the source code for these example programs from the Visual Studio Help menu.

These example programs are generally console applications, since these are concise complete programs. The code examples are ready for you to copy and paste into your own projects.

This procedure describes how to locate the Visual C++ code example programs in the help topics and how to compile and run them.

To copy example Visual C++ programs to the Clipboard

  1. On the Help menu, click Contents.

  2. Set the Filtered by list to Visual C++.

  3. Navigate to one of the example programs:

    In the Contents area, open the tree nodes for Development Tools and Languages, Visual Studio, Visual C++ and Programming Guide.

    If you are using Visual C++ Express Edition, open the nodes Express Library for Visual Studio, Visual C++ Express Edition, and Programming Guide.

  4. There are many example programs in this area of the help.

    For example, open .NET Programming in C++ and Windows Operations and select the How to: Retrieve Time Elapsed Since Startup topic.

    This opens the topic in a window in Visual Studio or in the Help Browser, depending on how your environment is configured.

  5. Click the Copy Code button to place the example program on the Clipboard.

  6. With the source code on the Clipboard, you can either:

    • Add a new .cpp file to the current project in Visual Studio and paste the source code into the file, then compile it in the Visual Studio Development Environment, as described in the How to: Compile a C++ Program that Targets the CLR in Visual Studio topic.

    • Or, paste the source code into a text editor and save it with a .cpp extension, then compile it on the command line, as described below.

Compiling the Code

Since the example programs are simple Visual C++ console applications, you may find it easier to compile them on the command line, as described in this procedure.

To compile a code example copied to the Clipboard

  1. Open the Visual Studio Command Prompt window by clicking the Start button, point to All Programs, Microsoft Visual Studio 2005, Visual Studio Tools, then click Visual Studio 2005 Command Prompt.

  2. At the command prompt, type the following and press Enter:

    notepad timeelapsed.cpp

    Click Yes when prompted to create a new file.

  3. On the Edit menu, click Paste to place the program you copied into Notepad.

  4. On the File menu, click Save.

  5. On the File menu, click Exit to exit Notepad.

  6. At the command line prompt, type the following and press Enter:

    cl /clr timeelapsed.cpp

  7. To run the timeelapsed.exe program, type timeelapsed and press Enter.

    The program runs and displays how much time has elapsed since Windows last started.

  8. Type exit and press Enter to close the command prompt window.

See Also