Installing MySQL Connector/C++ from Source
Before you proceed with the installation from source, check the Connector/C++ downloads page to see whether the driver is available in binary form for your build platform, and whether it works for you. In general, all the pre-compiled binaries are built with the best possible options for all the supported platforms. Installation from the binary package is very straight forward on all platforms - simply unpacking the zip archive in a desired location installs the Connector/C++ driver.
- Build-time Dependencies
- Install CMake
- Install MySQL Client Programming Support
- Build & Install MySQL Connector/C++
These instructions outline the dependencies and the steps to build and install the Connector/C++ driver on all supported platforms.
bash shell was used to show all the examples in this tutorial.
CMake 2.6.2 or later on Microsoft Windows, and version 2.4.2 or later on rest of the supported platforms
- CMake is a cross-platform build system that generates native build files for your environment.
MySQL client programming support
- Connector/C++ is based on the MySQL C API, and the driver is linked against the MySQL Client Library.
- GLib 2.2.3 or later on Linux
Check the CMake software downloads page for the pre-compiled binary for your build platform. If the binary is not available, or if you plan to build CMake from a source tree, check Installing CMake page for the instructions to build CMake.
The following example demonstrates the steps involved in building & installing CMake in a non-default location on Solaris operating system. Sun Studio C/C++ compilers were used to build CMake.
# gunzip -c cmake-2.6.3.tar.gz | tar -xvf - # cd cmake-2.6.3 # export CC=cc # export CXX=CC # ./bootstrap --prefix=/export/expts/CMake # make # make install # export PATH=/export/expts/CMake/bin:$PATH # which cmake /export/expts/CMake/bin/cmake # cmake -version cmake version 2.6-patch 3
Install MySQL Client Programming Support
MySQL Connector/C++ mandates the installation of MySQL 5.1 [or later] client programming support -- MySQL specific libraries and the header files. Majority of the MySQL Server installation methods on different platforms install the necessary files by default. However if you installed MySQL from RPM files on Linux, be sure that you've installed the developer RPM. The client programs are in the client RPM, but client programming support is in the developer RPM.
If you do not have the MySQL Server installed and if you only need the MySQL Client Library along with the header files, consider installing MySQL Connector/C on the build machine for the required client programming support. MySQL Connector/C is a C client library for client-server communication. It is a standalone replacement for the MySQL Client Library shipped with the MySQL Server. As of this writing, MySQL Connector/C 6.0 is available in the source form as well as the binary form for Solaris, Linux, Windows, FreeBSD, Mac OS X, HP-UX and AIX platforms. MSI installer and a binary zip file without the installer is available for Windows, where as the binary package is available as compressed GNU TAR archive (tar.gz) for the rest of the platforms. You can download the source code and the pre-compiled binary from the Connector/C 6.0 download page. Building the Connector/C driver from the source code is very similar to building the Connector/C++ driver from the source code.
If Connector/C does not fit your requirements, download MySQL, then install.
Add the location of the MySQL Client Library to the search path that the runtime linker uses to find dynamic dependencies. For example, on Solaris OS, use LD_LIBRARY_PATH environment variable to set the runtime linker search path as shown below.
Connector/C # ls -1 /export/expts/MySQLConnectorC6.0/lib/libmysqlclient_r.so /export/expts/MySQLConnectorC6.0/lib/libmysqlclient_r.so # export LD_LIBRARY_PATH=/export/expts/MySQLConnectorC6.0/lib:$LD_LIBRARY_PATH = OR = MySQL Server # ls -1 /opt/coolstack/mysql_32bit/lib/mysql/libmysqlclient_r.so.16.0.0 /opt/coolstack/mysql_32bit/lib/mysql/libmysqlclient_r.so.16.0.0 # export LD_LIBRARY_PATH=/opt/coolstack/mysql_32bit/lib/mysql:$LD_LIBRARY_PATH
Build & Install MySQL Connector/C++
The following step-by-step instructions are applicable to all supported UNIX like platforms. Check MySQL Forge Wiki and MySQL Connector/C++: Guide on building a Windows Client using Visual Studio (for Dummies) pages for the Windows build instructions.
Download the latest version of the MySQL Connector/C++ source tree from dev.mysql.com.
Extract the source files.
# gunzip -c mysql-connector-c++-1.0.4-beta.tar.gz | tar -xvf - # cd mysql-connector-c++-1.0.4-beta
If you are not using the GNU C/C++ compilers, you need to tell the Make build system which compiler to use by setting the environment variables
For example, to use Sun Studio C/C++ compilers on Solaris and Linux platforms, set the environment variables
CXXas shown below.
# which cc /usr/bin/cc # which CC /usr/bin/CC # export CC=cc # export CXX=CC
Run CMake, the cross-platform build system, to generate the
Makefile. In this step, you can specify the compiler and link-editor options. In addition, you can specify a variety of options like the installation directory, build type - debug or non-debug, location of
mysql_configin this step. For all the supported options and the exact syntax, check the CMake Documentation.
# cmake -DCMAKE_REQUIRED_FLAGS=-xO4 -DCMAKE_INSTALL_PREFIX=/export/expts/MySQLConnectorC++
On non-Windows systems, CMake tries to locate
mysql_configfile in the default locations, if the CMake variable
MYSQL_CONFIG_EXECUTABLEis not set. If the MySQL Server is installed in a non-default location, set the CMake variable
# cmake -DMYSQL_CONFIG_EXECUTABLE=/path/to/my/mysql/server/bin/mysql_config [.. other options ..]
On Windows platform,
mysql_configis not available. Hence CMake attempts to retrieve the location of MySQL from the environment variable
MYSQL_DIRvariable is not set, then CMake proceeds to check for MySQL in the following locations:
To build debug version of the MySQL Connector/C++, set the CMake variable
# cmake -DCMAKE_BUILD_TYPE=Debug [.. other options ..]
The MySQL Connector/C++ driver is capable of generating debug traces. By default, connector's internal tracing functionality is not available. If you need it, you have to enable the tracing module at compile time using the CMake option,
-DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1. The tracing functionality is available with both debug and non-debug builds of the connector, hence it is not required to build debug version of the MySQL Connector/C++ driver in order to use the internal tracing functionality of the connector.
To enable the tracing module, set the CMake variable
MYSQLCPPCONN_TRACE_ENABLEwith the type
BOOLand the value set to 1.
# cmake -DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1 [.. other options ..]
In addition to the internal debug traces, Connector/C++ can also generate debug traces when linked against the debug version of MySQL Client Library. Check Debug Tracing with MySQL Connector/C++ for more details.
cmake -Lto check the list of options being used by CMake.
maketo build the Connector/C++ driver.
installoption to install the Connector/C++ driver in the designated location.
# make install .. -- Installing: /export/expts/MySQLConnectorC++/lib/libmysqlcppconn.so.1 -- Installing: /export/expts/MySQLConnectorC++/lib/libmysqlcppconn-static.a -- Installing: /export/expts/MySQLConnectorC++/include/mysql_connection.h -- Installing: /export/expts/MySQLConnectorC++/include/mysql_driver.h ..
If the CMake variable
CMAKE_INSTALL_PREFIXwas not set during the
Makefilegeneration (step #4), the driver along with the header files will be installed in the default location,