C++ Compiler

Ubuntu

I originally built the examples in this book on Ubuntu 12.10 using g++ 4.7.2.

Install g++ using the following command:

 
sudo apt-get install build-essential

OS X

I successfully built the examples in this book on Mac OS X 10.8.3 (Mountain Lion) using a gcc port. The version of gcc shipped with Xcode at the time of this writing, 4.2, will not successfully compile the C++ examples in this book.

To install the gcc port, you may need to install MacPorts, an infrastructure that allows you to install free software onto your Mac. Refer to http://www.macports.org/install.php for further information.

You will want to first update MacPorts.

 
sudo port selfupdate

Install the gcc port using the following command:

 
sudo port install gcc47

This command may take a considerable amount of time to execute.

(If you prefer, you can specify the +universal variant at the end of the port command, which will enable compiling binaries for both PowerPC and Intel architectures.)

Once you have successfully installed the gcc port, indicate that its installation should be the default.

 
sudo port select gcc mp-gcc47

You may want to add the command to the path name list.

 
hash gcc

Windows

On Windows, your best bet for getting the code working as it appears in this book (and thus as it appears in the source distribution) is to consider a MinGW or Cygwin port of g++. Other avenues for exploration include the Microsoft Visual C++ Compiler November 2012 CTP and Clang, but at the time of this writing they do not provide sufficient support for the C+11 standard. This subsection will give a brief overview of some of the challenges and suggestions associated with getting the examples to run on Windows.

Visual C++ Compiler November 2012 CTP

You can download a community technology preview (CTP) release of the Visual C++11 compiler.[1] A Visual C++ Team Blog entry[2] describes the release.

A cursory look at using the CTP for this book’s examples quickly revealed a few things.

  • In-class member initialization does not yet appear to be fully supported.

  • Support for C++11 appears to be most deficient in the std library. For example, the collection classes do not yet support uniform initializer lists. There also appears to be no implementation for std::unordered_map.

  • Google Mock/Google Test uses variadic templates, which are not yet fully supported. You will receive a compilation error when building Google Mock. You’ll need to add an entry to your preprocessor definitions that sets _VARIADIC_MAX to 10 for all affected projects. Refer to http://stackoverflow.com/questions/12558327/google-test-in-visual-studio-2012 for further information on how to get past this problem.

Windows Example Code

As this book approaches final publication, efforts are underway to create working Windows code examples (by eliminating unsupported C++11 elements). You can find the reworked examples as a separate set of repositories at my GitHub page (https://github.com/jlangr), one repository per chapter. Refer to the Google Group discussion forum at https://groups.google.com/forum/?fromgroups#!forum/modern-cpp-with-tdd for further information about Windows examples as they get posted.

The Windows GitHub repositories contain solution (.sln) and project (.vcxproj) files. You should be able to use these files to load example code in Visual Studio Express 2012 for Windows Desktop. You can also use MSBuild to build and run tests for the examples from the command line.

If you want to rework the code examples on your own, it shouldn’t be too horrible an experience. Changing to out-of-class initialization should be easy. You can replace std::unordered_map with std::map. And many of the new additions to C++11 originated in the boost::tr1 library, so you might be able to directly substitute the Boost implementations.

A Few Windows Tips

I Googled my way through a number of roadblocks in the form of compilation warnings, errors, and other build challenges. Here are a few things I learned along the way:

Error/Challenge

Resolution

C297: ’std:tuple’: too many template arguments.

Add preprocessor definition for _VARIADIC_MAX=10. See http://stackoverflow.com/questions/8274588/c2977-stdtuple-too-many-template-arguments-msvc11.

Specified platform toolset (v110) is not installed or invalid.

Set VisualStudioVersion 11.0.

Where is msbuild.exe?

Mine is in c:WindowsMicrosoft.NETFrameworkv4.0.30319.

Warning C4996: ’std::_Copy_impl’: Function call with parameters that may be unsafe.

-D_SCL_SECURE_NO_WARNINGS

Console windows closes on completion of running tests with Ctrl-F5.

Set Configuration PropertiesLinkerSystemSubSystem to Console (/SUBSYSTEM:CONSOLE).

Visual Studio tries to autolink libraries for header-only Boost features.

Add BOOST_ALL_NO_LIB preprocessor directive.

Many of the resolutions for these challenges are already embodied in the project files.

Visual Studio 2013 Previews

Shortly before my final deadline for book changes, Microsoft released preview downloads for Visual Studio 2013, which promises additional compliance with the C++11 standard as well as support for some proposed C++14 features. In the short term, the Windows code at the GitHub site will work under the November 2012 CTP. But you’ll soon find updated versions that take even more advantage of C++11 as we (myself and a few great folks helping out) work with them under Visual Studio 2013. I’m hoping you eventually don’t find Windows-specific versions at all. Here’s to a fully C++11-compliant Windows compiler!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.154