Cover | Table of Contents
www.mingw.org, and follow the link to the
MinGW download page. Download the latest version of the MinGW
installation program, which should be named
MinGW-<version>.exe.PATH environment variable so that you can
specify these tools on the command line by their simple names rather
than by their full pathnames.www.cygwin.com, and follow the link
InstallCygwin Now to download
the Cygwin installation program. Next, run the installation program.
It will ask you to make a series of choices, such as where Cygwin
should be installed.hello.cpp
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
}
|
Toolset
|
Script
|
|---|---|
|
Visual C++
|
vcvars32.bat
|
|
Intel (Windows)
|
iclvars.bat
|
|
Intel (Linux)
|
iccvars.sh or iccvars.csh
|
|
Metrowerks (Mac OS X)
|
mwvars.sh or mwvars.csh
|
|
Metrowerks (Windows)
|
cwenv.bat
|
|
Comeau
|
Same as the backend toolset
|
|
Toolset
|
Command line
|
|---|---|
|
GCC (Unix)Intel (Linux)Comeau (Unix)
|
ar ru libjohnpaul.a john.o
paul.o
johnpaul.oranlib
libjohnpaul.a
|
|
GCC (Windows)
|
ar ru libjohnpaul.a john.o
paul.o
johnpaul.o
|
|
Visual C++Comeau (with Visual C++)
|
lib -nologo -out:libjohnpaul.lib john.obj
paul.obj
johnpaul.obj
|
|
Intel (Windows)
|
xilib -nologo /out:libjohnpaul.lib john.obj
paul.obj
johnpaul.obj
|
GEORGERINGO_DLL. If you're
building a third-party library, the installation instructions should
tell you what macros to define.|
Toolset
|
Command line
|
|---|---|
|
GCC
|
g++ -shared -fPIC -o libgeorgeringo.so george.o
ringo.o georgeringo.o
|
|
GCC (Mac OS X) |
|
Toolset
|
Option
|
|---|---|
|
All
|
-I<directory>
|
www.boost.org/boost-build2 or follow these
steps:www.boost.org, and
follow the Download link to Boost's
SourceForge download area.PATH environment variable.BOOST_BUILD_PATH to the Boost. Build root
directory. If you downloaded the package boost
in step 1, the root directory is the subdirectory
tools/build/v2 of your Boost installation;
otherwise, it is the directory boost-build.exe
rule to declare an executable target, specifying your
.cpp file as a source.
Next, invoke the install rule, specifying the
executable target name and the location where you want the install
directory. Finally, run bjam to build your
program.# jamfile for project hello exe hello : hello.cpp ; install dist : hello : <location>. ;
> bjam hello
> bjam dist
location property, which in this case is the
current directory.lib rule
to declare a library target, specifying your
.cpp files as sources and the property
<link>static as a requirement. Add a usage
requirement of the form
<include>path to
specify the library's include directory, i.e., the
directory with respect to which include directives
for library headers should be resolved. You may need to add one or
more requirements of the form
<include>path to
tell the compiler where to search for included headers. Finally, run
bjam from the directory containing
Jamroot, as described in Recipe 1.7.# Jamfile for project libjohnpaul
lib libjohnpaul
: # sources
john.cpp paul.cpp johnpaul.cpp
: # requirements
<link>static
: # default-build
: # usage-requirements
<include>..
;
> bjam libjohnpaul
lib rule is used to declare a target
representing a static or dynamic library. It takes the same form as
the exe rule, as illustrated in Example 1-9. The usage requirement
<include>.. frees projects that depend on
your library from having to explicitly specify your
library's include directory in their requirements.
The requirement <link>static specifies that
your target should always be built as a static library. If you want
the freedom to build a library target either as static or as dynamic,
you can omit the requirement <link>static.
Whether the library is built as static or dynamic can then be
specified on the command line, or in the requirements of a target
that depends on the library target. For example, if the requirement
lib rule to
declare a library target, specifying your .cpp
files as sources and the properties <link>shared
as a requirement. Add a usage requirement of the form
<include>path to
specify the library's include directory, i.e., the
directory with respect to which include directives
for library headers should be resolved. If your source files include
headers from other libraries, you may need to add several
requirements of the form
<include>path to
tell the compiler where to search for included headers. You may also
need to add one or more requirements of the form
<define>symbol to
ensure that your dynamic library's symbols will be
exported using _ _declspec(dllexport) on Windows.
Finally, run bjam from the directory containing
Jamroot, as described in Recipe 1.7.# Jamfile for project georgringo
lib libgeorgeringo
: # sources
george.cpp ringo.cpp georgeringo.cpp
: # requirements
<link>shared
<define>GEORGERINGO_DLL
: # default-build
: # usage-requirements
<include>..
;
> bjam libgeorgeringo
lib rule is used
to declare a target representing a static or dynamic library. The
usage requirement <include>.. frees projects
which depend on your library from having to explicitly specify your
library's include directory in their requirements.
The requirement exe rule to declare an executable target. Specify
your .cpp files and the library targets on which
the executable depends as sources. Also, add properties of the form
<include>path as
sources, if necessary, to tell the compiler where to search for
library headers.install rule, specifying the properties
<install-dependencies>on,
<install-type>EXE, and
<install-type>SHARED_LIB as requirements.# Jamfile for project hellobeatles
exe hellobeatles
: # sources
../johnpaul//libjohnpaul
../georgeringo//libgeorgeringo
hellobeatles.cpp
;
install dist
: # sources
hellobeatles
: # requirements
<install-dependencies>on
<install-type>EXE
<install-type>SHARED_LIB
<location>.
;
> bjam hellobeatles
hellobeatles depends, and then builds the target
hellobeatles. Finally, enter:> bjam dist
_
_declspec(dllexport).http://www.mingw.org, go to the MinGW
download area and download the latest stable version of the MSYS
installation program. The name of the installation program should be
MSYS-<version>.exe.PATH environment
variable.GNU Make 3.80 Copyright (C) 2002 Free Software Foundation, Inc. This is free software; see the source for copying conditions. ...
targets: prerequisitescommand-script
all, and specify the name of the executable you
wish to build as its sole prerequisite. It should have no command
script. Give the second target the same name as your executable.
Specify your application's source file as its
prerequisite, and specify the command line needed to build the
executable from the source file as your target's
command script. The third target should be called
install. It should have no prerequisites, and
should have a command script to copy the executable from the
directory containing the makefile to the directory where you want it
installed. The last target should be called clean.
Like install, it should have no prerequisites. Its command script
should remove the executable and the intermediate object file from
the current directory. The all whose single prerequisite is the static
library. Next, declare your static library target. Its prerequisites
should be the object files that the library will contain, and its
command script should be a command line to build the library from the
collection of object files, as demonstrated in Recipe 1.3. If you are using GCC or a compiler with
similar command-line syntax, customize the implicit patterns rules,
if necessary, by modifying one or more of the variables
CXX, CXXFLAGS,
etc. used in
make's database of implicit
rules, as shown in Recipe 1.15. Otherwise,
write a
pattern
rule telling make how to compile
.cpp files into object files, using the command
lines from Table 1-4 and the pattern rule syntax
explained in Recipe 1.16. Next, declare
targets indicating how each of your library's source
files depends on the headers it includes, directly or indirectly. You
can write these dependencies by hand or arrange for them to be
generated automatically. Finally, add install and
clean targets as demonstrated in Recipe 1.15.# Specify extensions of files to delete when cleaning
CLEANEXTS = o a
# Specify the target file and the install directory
OUTPUTFILE = libjohnpaul.a
INSTALLDIR = ../binaries
# Default target
.PHONY: all
all: $(OUTPUTFILE)
# Build libjohnpaul.a from john.o, paul.o, and johnpaul.o
$(OUTPUTFILE): john.o paul.o johnpaul.o
ar ru $@ $^
ranlib $@
# No rule to build john.o, paul.o, and johnpaul.o from .cpp
# files is required; this is handled by make's database of
# implicit rules
.PHONY: install
install:
mkdir -p $(INSTALLDIR)
cp -p $(OUTPUTFILE) $(INSTALLDIR)
.PHONY: clean
clean:
for file in $(CLEANEXTS); do rm -f *.$$file; done
# Indicate dependencies of .ccp files on .hpp files
john.o: john.hpp
paul.o: paul.hpp
johnpaul.o: john.hpp paul.hpp johnpaul.hppall whose single prerequisite is the dynamic
library. Next, declare your dynamic library target. Its prerequisites
should be the object files from which the library will be built, and
its command script should be a command line to build the library from
the collection of object files, as demonstrated in Recipe 1.4. If you are using GCC or a compiler with
similar command-line syntax, customize the implicit patterns rules,
if necessary, by modifying one or more of the variables
CXX, CXXFLAGS,
etc. used in
make's database of implicit
rules, as shown in Recipe 1.15. Otherwise,
write a pattern rule telling make how to compile
.cpp files into object files, using the command
lines from Table 1-4 and the pattern rule syntax
explained in Recipe 1.16. Finally, add
install and clean targets, as
demonstrated in Recipe 1.15, and
machinery to automatically generate source file dependencies, as
demonstrated in Recipe 1.16.# Specify extensions of files to delete when cleaning
CLEANEXTS = o so
# Specify the source files, the target files,
# and the install directory
SOURCES = george.cpp ringo.cpp georgeringo.cpp
OUTPUTFILE = libgeorgeringo.so
INSTALLDIR = ../binaries
.PHONY: all
all: $(OUTPUTFILE)
# Build libgeorgeringo.so from george.o, ringo.o,
# and georgeringo.o; subst is the search-and-replace
# function demonstrated in Recipe 1.16
$(OUTPUTFILE): $(subst .cpp,.o,$(SOURCES))
$(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^
.PHONY: install
install:
mkdir -p $(INSTALLDIR)
cp -p $(OUTPUTFILE) $(INSTALLDIR)
.PHONY: clean
clean:
for file in $(CLEANEXTS); do rm -f *.$$file; done
# Generate dependencies of .ccp files on .hpp files
include $(subst .cpp,.d,$(SOURCES))
%.d: %.cpp
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$all whose prerequisite is your executable. Declare
a target for your executable with prerequisites consisting of the
libraries which your application uses, together with the object files
to be built from your application's
.cpp files. Write a command script to build the
executable from the collection libraries and object files, as
described in Recipe 1.5. If necessary, write
a pattern rule to generate object files from
.cpp files, as shown in Recipe 1.16. Add install and
clean targets, as shown in Recipe 1.15, and
machinery to automatically generate source file dependencies, as
shown in Recipe 1.16.all whose
prerequisite is the directory containing the makefile created in step
2. Declare a rule whose targets consists of the directories
containing the subordinate makefiles, and whose command script
invokes make in each target directory with a
target specified as the value of the variable
TARGET. Finally, declare targets specifying the
dependencies between the default targets of the subordinate
makefiles.<define>name[=value]
to your target's requirements, as shown in Table 1-15 and Example 1-12.|
Toolset
|
Option
|
|---|---|
|
All
|
-D
name
[=
value
]
|
|
IDE
|
Configuration
|
|---|---|
|
Visual C++
|
From your project's property pages, go to
Configuration Properties →
C/C++ → Preprocessor and
enter
name[=value]
under Preprocessor Definitions, using semicolons to separate multiple
entries.
|
|
CodeWarrior
|
From the Target Settings Window, go to Language Settings
→ C/C++ Preprocessor and
enter:
#define
name[
=
value]
in the area labeled Prefix Text.
|
|
C++Builder
|
From Project Options, go to Directories/Conditionals and enter
name[=value]
under Preprocessor Definitions, using semicolons to separate multiple
entries. |