1.22. Producing a Release Build

Problem

You want to produce a small, fast executable or dynamic library for distribution to your customers.

Solution

In general, to produce a release build you must

  • Enable optimizations

  • Enable the expansion of inline function

  • Disable the generation of debugging information

Table 1-26 presents the compiler and linker options to enable optimization and inlining. There are no command-line options for disabling the generation of debugging information: when you build from the command line, debugging information is disabled by default. If you use the GCC toolset, however, you can decrease the size of executables and dynamics libraries by specifying the -s option to the linker.

Table 1-26. Compiler options to enable optimization and inlining

Toolset

Optimization

Inlining

GCC

-O3

-finline-functions [14]

Visual C++Intel

-O2

-Ob1

Metrowerks

-opt full

-inline auto -inline level=8

Comeau (Unix)

-O3

Comeau (Windows)

Same as backend, but using a slash (/) instead of a dash (-)

—inlining

Borland

-O2

-vi

Digital Mars

-o+time

Enabled by default

[14] This option is enabled automatically when -O3 is specified.

Boost.Build provides a simple mechanism for producing a release build: simply add <variant>release to your target’s requirements or use the command-line option variant=release, which can be abbreviated simply as release.

Some IDEs also provide a simple way to produce a release build. For instance, as I mentioned in Recipe 1.21, when you create a new project with Visual C++, the IDE generates debug and release configurations automatically. To request a release build, simply select Configuration Manager... from the Build menu and select Release as the active configuration. You can also select Release from the drop-down list of configurations on the standard toolbar. The next time you build your project, it will produce a release build.

Similarly, when you create a new project with CodeWarrior using one of Metrowerks’s project templates, called stationery, the IDE generates debug and release targets automatically. The name of the release target may vary, but it should always contain the word “release” or “final.” To request a release build, select Set Default Target from the Project menu, and then select the menu item corresponding to the release target. You can also select the release target from the drop-down list of targets on your project’s window.

C++Builder does not support multiple build configurations for a single project, but it does provide an easy way produce a release build. To request a release build, go to Project Options Compiler and press Release. This will enable optimization and inlining and disable debugging information.

If you are using an IDE which doesn’t provide preset debug and release configurations, such as Dev-C++, or if you need more control over your project’s settings, refer to Tables 1-27 through 1-29.

Table 1-27. Enabling optimization from your IDE

IDE

Configuration

Visual C++

From your project’s property pages, go to Configuration Properties C/C++ Optimization and set Optimization to Maximize Speed, Favor Size or Speed to Favor Fast Code, and Global Optimizations, Enable Intrinsic Functions, and Omit Frame Pointers to Yes. Use the default settings for the other properties on this page.

CodeWarrior

From the Target Settings Window, go to Code Generation Global Optimizations and select Level 4.

C++Builder

From Project Options, go to Compiler and select Speed under Code optimization.

Dev-C++

See the entry for GCC in Table 1-26 and refer to Recipe 1.20.

Table 1-28. Enabling inlining from your IDE

IDE

Configuration

Visual C++

From your project’s property pages, go to Configuration Properties C/C++ Optimization and set Inline Function Expansion to Any Suitable.

CodeWarrior

From the Target Settings Window, go to Language Settings C/C++ Language. Set Inline Depth to 8, check Auto-Inline and leave the other inlining options unchecked.

C++Builder

From Project Options, go to Compiler and uncheck Disable inline expansions under Debugging.

Dev-C++

See the entry for GCC in Table 1-26 and refer to Recipe 1.20.

Table 1-29. Disabling debug information from your IDE

IDE

Configuration

Visual C++

From your project’s property pages, go to Configuration Properties C/C++ General and select Disabled as the Debug Information Format

Metrowerks

From the Target Settings Window, go to Language Settings Linker x86 Linker and uncheck Store full paths, Link debug info, and Debug inline functions.

C++Builder

From Project Options, go to Compiler and uncheck Debug Information and Line Number Information.

Dev-C++

Make sure that the command-line option -g has not been specified, as described in Recipe 1.20.

Discussion

Most toolsets offer several options for optimization; some offer dozens. Which optimizations you choose depends heavily on the requirements of your project. In an embedded environment, for example, you may want to pick an optimization that produces a smaller executable at the expense of some speed. In other cases, speed may be paramount. Some optimizations will make your program faster on one platform but slower on another. You might even find that certain options make parts of your program faster and other parts slower.

While Table 1-26 and Table 1-27 present good general-purpose optimization options, for best results you should carefully consider your requirements, study your toolset’s documentation, and conduct extensive tests.

This situation is similar with inlining, although toolsets usually provide fewer options for inlining than for other optimizations.

See Also

Recipe 1.21

Get C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.