Chapter 4 Installing Connector/C++ from Source

Table of Contents

4.1 Source Installation System Prerequisites
4.2 Obtaining and Unpacking a Connector/C++ Source Distribution
4.3 Installing Connector/C++ from Source on Unix and Unix-Like Systems
4.4 Installing Connector/C++ from Source on Windows
4.5 Troubleshooting Connector/C++ Source Installation Problems
4.6 Dynamically Linking Connector/C++ Against the MySQL Client Library
4.7 Connector/C++ Source-Configuration Options

This chapter describes how to install Connector/C++ using a source distribution.

4.1 Source Installation System Prerequisites

To install Connector/C++ from source, the following system requirements must be satisfied:

  • To compile Connector/C++, the MySQL client library (MySQL C API) must be installed. Connector/C++ is based on the MySQL client library and is linked against it.

    Typically, the MySQL client library is installed when the MySQL Server is installed. However, check your operating system documentation for other installation options. Alternatively, you can install the client library by installing Connector/C.

    You must use either a General Availability version of MySQL 5.7 (5.7.9 or higher) or Connector/C 6.1.8 or higher. Set the MYSQL_DIR CMake option appropriately at configuration time as necessary.

  • You must have the cross-platform build tool CMake 2.6.2, or newer, and GLib 2.2.3 or newer. Check the README file included with the distribution for platform-specific notes.

  • The Boost C++ libraries are required to build Connector/C++ (but not to use it). Boost 1.56.0 or newer must be installed. To obtain Boost and its installation instructions, visit the official site. After Boost is installed, tell the build system where the Boost files are located by defining the BOOST_ROOT option when you invoke CMake. For example:

    shell> cmake . -DBOOST_ROOT=/usr/local/boost_1_56_0
    

    Adjust the path as necessary to match your installation.

4.2 Obtaining and Unpacking a Connector/C++ Source Distribution

To obtain a Connector/C++ source package, visit the Connector/C++ downloads page.

A Connector/C++ source distribution is packaged as a compressed tar file or Zip archive, denoted here as PACKAGE.tar.gz or PACKAGE.zip. A source distribution in tar file or Zip archive format can be used on any supported platform.

To unpack a compressed tar file, use this command in the intended installation directory:

shell> tar zxvf PACKAGE.tar.gz

After unpacking the distribution, build it using the appropriate instructions for your platform later in this section.

To unpack a Zip archive, use WinZip or another tool that can read .zip files. After unpacking the distribution, build it using the appropriate instructions for your platform later in this section.

To check out the Connector/C++ code from the source code repository located on GitHub at https://github.com/mysql/mysql-connector-cpp;, use the following command:

shell> git clone https://github.com/mysql/mysql-connector-cpp

4.3 Installing Connector/C++ from Source on Unix and Unix-Like Systems

To install Connector/C++ from source, your system must satisfy the requirements outlined in Section 4.1, “Source Installation System Prerequisites”.

  1. Change location to the top-level directory of the source distribution:

    shell> cd /path/to/mysql-connector-cpp
    
  2. Run CMake to build a Makefile:

    shell> cmake .
    -- Check for working C compiler: /usr/local/bin/gcc
    -- Check for working C compiler: /usr/local/bin/gcc -- works
    [...]
    -- Generating done
    -- Build files have been written to: /path/to/mysql-connector-cpp/
    

    To use configuration values different from the defaults, use the options described at Section 4.7, “Connector/C++ Source-Configuration Options”.

    On non-Windows systems, CMake checks to see whether the MYSQL_CONFIG_EXECUTABLE CMake option is set. If not, CMake tries to locate mysql_config in the default locations.

    If you have any problems with the configuration process, check the troubleshooting instructions given in Section 4.5, “Troubleshooting Connector/C++ Source Installation Problems”.

  3. Use make to build Connector/C++. First make sure you have a clean build, then build the connector:

    shell> make clean
    shell> make
    [  1%] Building CXX object »
    driver/CMakeFiles/mysqlcppconn.dir/mysql_connection.o
    [  3%] Building CXX object »
    driver/CMakeFiles/mysqlcppconn.dir/mysql_constructed_resultset.o
    [...]
    [100%] Building CXX object examples/CMakeFiles/statement.dir/statement.o
    Linking CXX executable statement
    

    If all goes well, you will find the Connector/C++ library in the driver directory.

  4. Install the header and library files:

    shell> make install
    

    Unless you have changed the location in the configuration step, make install copies the header files to the directory /usr/local/include. The header files copied are mysql_connection.h and mysql_driver.h.

    Again, unless you have specified otherwise, make install copies the library files to /usr/local/lib. The files copied are the dynamic library libmysqlcppconn.so, and the static library libmysqlcppconn-static.a. The dynamic library file name extension might differ on your system (for example, .dylib on OS X).

After installing Connector/C++, you can carry out a quick test to check the installation. To do this, compile and run one of the example programs, such as examples/standalone_example.cpp. This example is discussed in more detail later, but for now, you can use it to test whether the connector has been correctly installed. This procedure assumes that you have a working MySQL Server to which you can connect. It also assumes header and library locations of /usr/local/include and /usr/local/lib, respectively; adjust these as necessary for your system.

  1. Compile the example program. To do this, change location to the examples directory and enter this command:

    shell> g++ -o test_install \
      -I/usr/local/include -I/usr/local/include/cppconn \
      -Wl,-Bdynamic -lmysqlcppconn standalone_example.cpp
    
  2. Make sure the dynamic library which is used in this case can be found at runtime:

    shell> export LD_LIBRARY_PATH=/usr/local/lib
    

    On OS X, try this:

    shell> export DYLD_LIBRARY_PATH=/usr/local/lib
    
  3. Now run the program to test your installation, substituting the appropriate host, user, password, and database arguments for your system:

    shell> ./test_install localhost root password database
    

    You should see output similar to the following:

    Connector/C++ standalone program example...
    
    ... running 'SELECT 'Welcome to Connector/C++' AS _message'
    ... MySQL replies: Welcome to Connector/C++
    ... say it again, MySQL
    ....MySQL replies: Welcome to Connector/C++
    
    ... find more at http://www.mysql.com
    

    If you see any errors, take note of them and go through the troubleshooting procedures in Section 4.5, “Troubleshooting Connector/C++ Source Installation Problems”.

4.4 Installing Connector/C++ from Source on Windows

To install Connector/C++ from source, your system must satisfy the requirements outlined in Section 4.1, “Source Installation System Prerequisites”.

Note

On Windows, mysql_config is not present, so CMake attempts to retrieve the location of MySQL from the environment variable $ENV{MYSQL_DIR}. If MYSQL_DIR is not set, CMake checks for MySQL in the following locations: $ENV{ProgramFiles}/MySQL/*/include and $ENV{SystemDrive}/MySQL/*/include.

Consult the CMake manual or check cmake --help to find out which build systems are supported by your CMake version:

C:\> cmake --help
cmake version 2.6-patch 2
Usage
[...]
Generators

The following generators are available on this platform:
  Borland Makefiles           = Generates Borland makefiles.
  MSYS Makefiles              = Generates MSYS makefiles.
  MinGW Makefiles             = Generates a make file for use with
                                mingw32-make.
  NMake Makefiles             = Generates NMake makefiles.
  Unix Makefiles              = Generates standard UNIX makefiles.
  Visual Studio 6             = Generates Visual Studio 6 project files.
  Visual Studio 7             = Generates Visual Studio .NET 2002 project
                                files.
  Visual Studio 7 .NET 2003   = Generates Visual Studio .NET 2003 project
                                files.
  Visual Studio 8 2005        = Generates Visual Studio .NET 2005 project
                                files.
  Visual Studio 8 2005 Win64  = Generates Visual Studio .NET 2005 Win64
                                project files.
  Visual Studio 9 2008        = Generates Visual Studio 9 2008 project fil
  Visual Studio 9 2008 Win64  = Generates Visual Studio 9 2008 Win64 proje
                                files.
[...]

It is likely that your CMake binary supports more compilers, known by CMake as generators, than can actually be used to build Connector/C++. We have built the connector using the following generators:

  • Microsoft Visual Studio 2013

  • NMake

CMake makes it easy for you to try other compilers. However, you may experience compilation warnings or errors, or linking issues not detected by Visual Studio. Patches are gratefully accepted to fix issues with other compilers.

For troubleshooting and configuration hints, see Section 4.5, “Troubleshooting Connector/C++ Source Installation Problems”.

Use these steps to build Connector/C++:

  1. Change location to the top-level directory of the source distribution:

    C:\> cd C:\path_to_mysql_cpp
    
  2. Run CMake to generate build files for your generator.

    To use configuration values different from the defaults, use the options described at Section 4.7, “Connector/C++ Source-Configuration Options”.

    Visual Studio

     C:\>cmake -G "Visual Studio 12 2013"
    -- Check for working C compiler: cl
    -- Check for working C compiler: cl -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working CXX compiler: cl
    -- Check for working CXX compiler: cl -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- ENV{MYSQL_DIR} =
    -- MySQL Include dir: C:/Programs/MySQL/MySQL Server 5.5/include
    -- MySQL Library    : C:/Programs/MySQL/MySQL Server 5.5/lib/opt/mysqlclient.lib
    -- MySQL Library dir: C:/Programs/MySQL/MySQL Server 5.5/lib/opt
    -- MySQL CFLAGS:
    -- MySQL Link flags:
    -- MySQL Include dir: C:/Programs/MySQL/MySQL Server 5.5/include
    -- MySQL Library dir: C:/Programs/MySQL/MySQL Server 5.5/lib/opt
    -- MySQL CFLAGS:
    -- MySQL Link flags:
    -- Configuring cppconn
    -- Configuring test cases
    -- Looking for isinf
    -- Looking for isinf - not found
    -- Looking for isinf
    -- Looking for isinf - not found.
    -- Looking for finite
    -- Looking for finite - not found.
    -- Configuring C/J junit tests port
    -- Configuring examples
    -- Configuring done
    -- Generating done
    -- Build files have been written to: C:\path_to_mysql_cpp
    C:\> dir *.sln *.vcproj
    [...]
    19.11.2008  12:16            23.332 MYSQLCPPCONN.sln
    [...]
    19.11.2008  12:16            27.564 ALL_BUILD.vcproj
    19.11.2008  12:16            27.869 INSTALL.vcproj
    19.11.2008  12:16            28.073 PACKAGE.vcproj
    19.11.2008  12:16            27.495 ZERO_CHECK.vcproj
    

    NMake

    C:\> cmake -G "NMake Makefiles"
    -- The C compiler identification is MSVC
    -- The CXX compiler identification is MSVC
    [...]
    -- Build files have been written to: C:\path_to_mysql_cpp
    
  3. Use your compiler to build Connector/C++.

    Visual Studio

    Open the newly generated project files in the Visual Studio GUI or use a Visual Studio command line to build the driver. The project files contain a variety of different configurations, debug and nondebug versions among them.

    For example, to use the command line, execute commands like these to build the distribution and create a Zip package:

    C:\> devenv MYSQLCPPCONN.sln /build RelWithDebInfo
    
    C:\> cpack -G ZIP --config CPackConfig.cmake -C RelWithDebInfo
    CPack: Create package using ZIP
    CPack: Install projects
    CPack: - Install project: MYSQLCPPCONN
    CPack: Create package
    CPack: - package: C:/opt/build/mysql-connector-c++-1.1.5/mysql-connector-c++-1.1.7-win32.zip generated.
    

    NMake

    C:\>nmake
    
    Microsoft (R) Program Maintenance Utility Version 9.00.30729.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    Scanning dependencies of target mysqlcppconn
    [  2%] Building CXX object driver/CMakeFiles/mysqlcppconn.dir/mysql_connection.obj
    mysql_connection.cpp
    [...]
    Linking CXX executable statement.exe
    [100%] Built target statement
    

4.5 Troubleshooting Connector/C++ Source Installation Problems

If you encounter problems building Connector/C++, try these troubleshooting strategies:

  1. For configuration or compilation problems, check your CMake options (MySQL installation path, debug version, and so forth):

    shell> cmake -L
    [...]
    CMAKE_BACKWARDS_COMPATIBILITY:STRING=2.4
    CMAKE_BUILD_TYPE:STRING=
    CMAKE_INSTALL_PREFIX:PATH=/usr/local
    EXECUTABLE_OUTPUT_PATH:PATH=
    LIBRARY_OUTPUT_PATH:PATH=
    MYSQLCPPCONN_GCOV_ENABLE:BOOL=0
    MYSQLCPPCONN_TRACE_ENABLE:BOOL=0
    MYSQL_CONFIG_EXECUTABLE:FILEPATH=/usr/local/mysql/bin/mysql_config
    

    For example, if your MySQL Server installation path is not /usr/local/mysql and you want to build a debug version of the Connector/C++, use this command:

    shell> cmake \
      -DCMAKE_BUILD_TYPE=Debug \
      -DMYSQL_CONFIG_EXECUTABLE=/path/to/my/mysql/server/bin/mysql_config .
    
  2. Use cmake -L to verify that your settings were changed:

    shell> cmake -L
    [...]
    CMAKE_BACKWARDS_COMPATIBILITY:STRING=2.4
    CMAKE_BUILD_TYPE:STRING=Debug
    CMAKE_INSTALL_PREFIX:PATH=/usr/local
    EXECUTABLE_OUTPUT_PATH:PATH=
    LIBRARY_OUTPUT_PATH:PATH=
    MYSQLCPPCONN_GCOV_ENABLE:BOOL=0
    MYSQLCPPCONN_TRACE_ENABLE:BOOL=0
    MYSQL_CONFIG_EXECUTABLE=/path/to/my/mysql/server/bin/mysql_config
    

    Proceed by executing a make clean command followed by a make command, as described previously.

4.6 Dynamically Linking Connector/C++ Against the MySQL Client Library

Note

This section refers to dynamic linking of Connector/C++ to the client library, not dynamic linking of applications to Connector/C++. Precompiled binaries of Connector/C++ use static binding with the client library by default.

An application that uses Connector/C++ can be either statically or dynamically linked to the Connector/C++ libraries. Connector/C++ is usually statically linked to the underlying MySQL client library (or to Connector/C; unless otherwise stated, reference to the MySQL client library is also taken to include Connector/C, which is a separately packaged, standalone version of the MySQL client library).

As of Connector/C++ 1.1.0, it is also possible to dynamically link to the underlying MySQL client library, although this capability is not enabled by default. To dynamically link Connector/C++ to the client library, enable MYSQLCLIENT_STATIC_LINKING when building Connector/C++ from source:

shell> rm CMakeCache.txt
shell> cmake . -DMYSQLCLIENT_STATIC_LINKING=1
shell> make clean
shell> make
shell> make install

Now, when creating a connection in your application, Connector/C++ will select and load a client library at runtime. It will choose the client library by searching defined locations and environment variables depending on the host operating system. It is also possible when creating a connection in an application to define an absolute path to the client library to load at runtime. This can be convenient if you have defined a standard location from which you want the client library to be loaded. This is sometimes done to circumvent possible conflicts with other versions of the client library that may be located on the system.

4.7 Connector/C++ Source-Configuration Options

Connector/C++ recognizes the CMake options described in this section.

Table 4.1 Connector/C++ Source-Configuration Option Reference

FormatsDescriptionDefaultIntroduced
BOOST_ROOTThe Boost source directory  
CMAKE_BUILD_TYPEType of build to produceRelWithDebInfo 
CMAKE_ENABLE_C++11Whether to enable C++11 supportOFF1.1.6
CMAKE_INSTALL_PREFIXInstallation base directory/usr/local 
MYSQLCLIENT_NO_THREADSWhether to link against single-threaded MySQL client libraryOFF 
MYSQLCLIENT_STATIC_LINKINGWhether to statically link to the MySQL client libraryOFF 
MYSQLCPPCONN_GCOV_ENABLEWhether to enable gcov supportOFF 
MYSQLCPPCON_TRACE_ENABLEWhether to enable tracing functionalityOFF 
MYSQL_CFLAGSC compiler flags  
MYSQL_CONFIG_EXECUTABLEPath to the mysql_config program${MYSQL_DIR}/bin/mysql_config 
MYSQL_CXXFLAGSC++ compiler flags  
MYSQL_CXX_LINKAGEWhether MySQL client library needs C++ linkingON 
MYSQL_DIRMySQL Server or Connector/C installation directory  
MYSQL_EXTRA_LIBRARIESExtra link libraries  
MYSQL_INCLUDE_DIRThe MySQL header file directory${MYSQL_DIR}/include 
MYSQL_LIB_DIRThe MySQL client library directory${MYSQL_DIR}/lib 
MYSQL_LINK_FLAGSExtra link flags  
USE_SERVER_CXXFLAGSUse MySQL Server CXXFLAGS value rather than system defaultOFF1.1.7

  • -DBOOST_ROOT=dir_name

    The directory where the Boost sources are installed.

  • -DCMAKE_BUILD_TYPE=type

    The type of build to produce:

    • RelWithDebInfo: Enable optimizations and generate debugging information. This is the default Connector/C++ build type.

    • Debug: Disable optimizations and generate debugging information.

  • -DCMAKE_ENABLE_C++11=bool

    Whether to enable C++11 support. The default is OFF.

    This option was added in Connector/C++ 1.1.6.

  • -DCMAKE_INSTALL_PREFIX=dir_name

    The installation base directory.

  • -DMYSQLCLIENT_NO_THREADS=bool

    Whether to link against a single-threaded libmysqlclient MySQL client library. In MySQL 5.5 and higher, the value of this option makes no difference because libmysqlclient is always multi-threaded.

  • -DMYSQLCLIENT_STATIC_LINKING=bool

    Whether to statically link to the MySQL client library. The default is OFF (use dynamic linking to the client library). For more information, see Section 4.6, “Dynamically Linking Connector/C++ Against the MySQL Client Library”.

  • -DMYSQLCPPCONN_GCOV_ENABLE=bool

    Whether to enable gcov support.

  • -DMYSQLCPPCON_TRACE_ENABLE=VALUE_TYPE

    Whether to enable tracing functionality. For information about tracing, see Chapter 9, Connector/C++ Debug Tracing.

  • -DMYSQL_CFLAGS=flags

    C compiler flags.

  • -DMYSQL_CONFIG_EXECUTABLE=file_name

    The path to the mysql_config program.

    On non-Windows systems, CMake checks to see whether the MYSQL_CONFIG_EXECUTABLE CMake option is set. If not, CMake tries to locate mysql_config in the default locations.

  • -DMYSQL_CXXFLAGS=flags

    C++ compiler flags.

  • -DMYSQL_CXX_LINKAGE=bool

    Whether the MySQL client library needs C++ linking.

  • -DMYSQL_DIR=dir_name

    The directory where MySQL Server or Connector/C is installed.

  • -DMYSQL_EXTRA_LIBRARIES=flags

    Any needed additional link libraries.

  • -DMYSQL_INCLUDE_DIR=dir_name

    The directory where the MySQL header files are installed.

  • -DMYSQL_LIB_DIR=dir_name

    The directory where the MySQL client library is installed.

  • -DMYSQL_LINK_FLAGS=flags

    Any needed additional link flags.

  • -DUSE_SERVER_CXXFLAGS=bool

    Use MySQL Server CXXFLAGS values rather than the system default value.

    This option was added in Connector/C++ 1.1.7.