Skip to Main Content
C++ Cookbook
book

C++ Cookbook

by D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, Jeff Cogswell
November 2005
Beginner to intermediate content levelBeginner to intermediate
594 pages
16h 23m
English
O'Reilly Media, Inc.
Content preview from C++ Cookbook

1.2. Building a Simple “Hello, World” Application from the Command Line

Problem

You want to build a simple “Hello, World” program, such as that in Example 1-4.

Example 1-4. A simple “Hello, World” program

hello.cpp

#include <iostream>

int main()
{
    std::cout << "Hello, World!\n";
}

Solution

Follow these steps:

  1. Set any environment variables required by your toolset.

  2. Enter a command telling your compiler to compile and link your program.

Scripts for setting environment variables are listed in Table 1-5; these scripts are located in the same directory as your command-line tools (Table 1-3). If your toolset does not appear in Table 1-5, you can skip the first step. Otherwise, run the appropriate script from the command line, if you are using Windows, or source the script, if you are using Unix.

Table 1-5. Scripts for setting environment variables required by your command-line tools

Toolset

Script

Visual C++

vcvars32.bat

Intel (Windows)

iclvars.bat [2]

Intel (Linux)

iccvars.sh or iccvars.csh

Metrowerks (Mac OS X)

mwvars.sh or mwvars.csh[3]

Metrowerks (Windows)

cwenv.bat

Comeau

Same as the backend toolset

[2] With earlier version of the Intel compiler, this script was named iccvars.bat.

[3] In versions of CodeWarrior prior to 10.0, there was a single csh script named mwvars.

Commands for compiling and linking hello.cpp are given in Table 1-6. To work properly, these commands require that your current directory is the directory containing hello.cpp and that the directory containing ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ System Programming Cookbook

C++ System Programming Cookbook

Onorato Vaticone

Publisher Resources

ISBN: 0596007612Supplemental ContentErrata Page