MySQL Connector/C++ 9.1.0
MySQL connector library for C and C++ applications
|
It is easiest to use Connector/C++ from a CMake project. For any target that uses Connector/C++ it is enough to link that target with the mysql::concpp
target defined after loading the mysql-concpp
module into the project, as in this example:
This ensures correct compiler and linker flags when building my-target
so that connector public headers, the libraries and their dependencies are found when compiling and linking the target. See the CMake documentation for more information on find_module()
command and on using CMake modules.
We focus here on the scenario where Connector/C++ is installed from a package published by MySQL. There are two variants of this scenario. Either the connector is installed system-wide via package manager such as RPM or DEB, or it is installed locally from TGZ or ZIP package. In case of system-wide installation connector components are installed to default locations and things are expected to work out-of-the-box. In this case find_module(mysql-concpp)
command should work without any further configuration.
In case of local installation you need to tell CMake where to look for the mysql-concpp
module. This can be done by setting the mysql-concpp_DIR
variable to the top-level Connector/C++ install location:
(Alternatively the variable can be set when invoking cmake
using -Dmysql-concpp_DIR=/path/to/concpp/install
option).
mysql-concpp
cmake module is defined starting from version 8.3.0. For earlier versions of Connector/C++ one has to configure build manually setting correct include paths, link libraries etc. See Manual build configurationConnector/C++ implements three APIs for communicating with MySQL Server. Two of them, X DevAPI for applications written in C++ and X DevAPI for C, access the document store of MySQL Server 8 or later via CRUD operations and they can also execute traditional SQL queries. The classic C++ API based on JDBC4 works over classic protocol and can communicate with MySQL Servers earlier than version 8.
The choice of the API is done by including appropriate public headers:
When using the classic API link your targets with the -jdbc variant of the connector target:
<mysql/jdbc.h>
header is available starting from version 8.0.16. For earlier versions the following set of headers needs to be included instead: <mysql/cppconn/*.h>
pattern depends on the APIs used in the code.Public headers define MYSQL_CONCPP_VERSION_NUMBER
macro that represents the Connector/C++ version number in XYYZZZZ format. For example for Connector/C++ 9.2.0 the macro will have the value 9020000.
Connector/C++ applications that use X DevAPI, X DevAPI for C, or the JDBC API can use the MYSQL_CONCPP_VERSION_NUMBER
macro to add conditional tests that determine the inclusion or exclusion of API features based on which Connector/C++ version introduced them.
Apart from MYSQL_CONCPP_VERSION_NUMBER
there are also macros with individual components of the version number: MYSQL_CONCPP_VERSION_MAJOR
, MYSQL_CONCPP_VERSION_MINOR
and MYSQL_CONCPP_VERSION_MICRO
.
Connector targets defined by mysql-concpp
module refer to shared connector libraries and imply dynamic linking. Depending on the platform, the shared Connector/C++ library is named:
libmysqlcppconnx.so
on Unix platforms (soname libmysqlcppconnx.so.B
)libmysqlcppconnx.dylib
on the OSX platform (link name libmysqlcppconnx.B.dylib
)mysqlcppconnx-B-vs14.dll
on Windows platforms (with import library vs14/mysqlcppconnx.lib).The classic API implementation is placed in a separate library which has libmysqlcppconn
as the base name: libmysqlcppconn.so
, libmysqlcppconn.dylib
or mysqlcppconn-B-vs14.dll
. Number B in the library name is the ABI version number and the ABI versions used when building the application and when running it must match.
-vs14
suffix used in Windows names indicates supported Microsoft Visual Studio toolchain version and is there to allow installing and using on the same computer connector libraries targeted at different VS versions. Currently Connector/C++ supports VS2019 and VS2022 and they both use the same major toolchain version 14. Therefore only -vs14
variants of libraries are shipped at the moment.MYSQL_CONCPP_RUNTIME_LIBRARY_DIR
variable which is set by the mysql-concpp
module. The variable is not set for system-wide installations in which connector libraries are installed at default locations.It is possible to link user code with the static version of connector libraries. The mysql-concpp
module defines targets mysql::concpp-static
and mysql::concpp-jdbc-static
that can be used for that purpose. In that case the resulting code does not require connector libraries to be present at run-time (however, their dependencies must still be available – see below).
-static
targets are not defined by the mysql-concpp
module and build configuration will fail if they are used. It is possible to detect this situation as follows: -jdbc
targets will not be defined.If both release and debug variants of the connector are available at the install location targets defined by the mysql-concpp
module will use the appropriate variant depending on the build configuration of the project.
If only release variants of the connector are available they will be used for both release and debug builds on non-Windows platforms. On Windows it is not possible to mix code built in release and debug modes. Connector targets defined by the mysql-concpp
module are configured such that building in debug mode will fail if only release libraries are available (linker will report missing libraires).
Standard Connector/C++ packages published by MySQL contain only release builds of the connector. To have debug variants, additional debug package needs to be installed on top of the regular one.
When loading the mysql-concpp
module a "debug" component can be required so that cmake will report error if debug variants of connector libraries are not found:
Regardless of whether the "debug" component was required or not, the module will set variable MYSQL_CONCPP_DEBUG_FOUND
to true if debug libraries were found.
Connector/C++ libraries depend on OpenSSL. For system-wide installations this dependency is resolved by ensuring that required version of OpenSSL is installed on the system as well. For local installations the OpenSSL libraries are bundled with the connector.
To use a different from the default installation of OpenSSL libraries define target mysql::openssl
that points at the custom installation to be used. For example:
Note that mysql::openssl
target must be defined before invoking find_module(mysql-concpp)
in order to replace the default OpenSSL instance used by the module.
The JDBC connector library might additionally depend on the MySQL client library. This is the case for system-wide installation where it is also ensured that the client library is installed on the system together with the connector. In case of local installations the client library is statically linked into the connector so that there is no external dependency.
However, the dependency might fail to be resolved at build-time if the client library is installed at some non-standard location. In that case it is necessary to set WITH_MYSQL
variable to point at the MySQL install location where the client library can be found:
The variable must be set before invoking find_package(mysql-concpp)
command. If the variable is not set then it is expected that the MySQL client library will be found at standard locations examined by the linker – otherwise linking of the code will fail.
An application that dynamically links to a shared connector library must have this library available at run-time together with its dependencies. If application is linked with the connector statically the connector library is not required at run-time but its dependencies still must be present.
This requirement is most easily satisfied if Connector/C++ is installed system-wide in which case all run-time dependencies will be installed as well. In case of local installation or when installing on Windows connector libraries and their dependencies might be not found by dynamic linker even though they are present at the install location.
To fix this problem on Windows the easiest (and most common) solution is to copy the DLLs provided by the connector package (both the connector DLL and the dependencies) to the location from which the application is run. This guarantees that they will be found by the dynamic linker. Another possibility is to add the location of connector DLLs to the executable path.
On non-Windows platforms, if connector was installed to a non-standard location, one can also copy connector libraries and their dependencies next to the application. Another alternative is to set LD_LIBRARY_PATH
or otherwise configure the dynamic linker to find required libraries at the non-standard location.
The classic API is implemented using the MySQL client library which might need to load plugins during some operations (mainly during authentication). In case of system-wide installation the required plugins (and their dependencies) should be also installed system-wide and available at the default location where the client library looks for them.
If however Connector/C++ was installed to a local directory the plugins (and their dependencies) are installed together with the connector at ${mysql-concpp_DIR}/lib64/plugins
but the client library will not know how to find them. To fix this pass their location as PLUGIN_DIR
connection option that is recognized by the classic API.
PLUGIN_DIR
must be correctly set (if needed) on the target host on which the application is run. This might be different from the environment in which application was built. Note that client library plugins are loaded dynamically at run-time and they are not required during build process.authentication_kerberos_client
plugin depends on Kerberos libraries and when used these libraries must be found on the target system.mysql-concpp
module sets the MYSQL_CONCPP_PLUGIN_DIR
variable to the location where bundled client library plugins (and their dependencies) are located inside connector installation directory. Depending on the deployment strategy this information can be used, for example, to bundle the available plugins with the final application. In case of system-wide installation the plugins are not bundled with the connector and MYSQL_CONCPP_PLUGIN_DIR
variable is not set.It is difficult to correctly set all required compiler and linker flags, especially in case of static linking. For that reason we highly recommend to use CMake for builds depending on Connector/C++.
If CMake is not an option then main settings needed for dynamic linking are the following:
${mysql-concpp_DIR}/include
directory is added to the compiler include path.libmysqlcppconnx.so
(libmysqlcppconnx.dylib
in case of MacOS) is added to linker inputs. For the library implementing the classic API the names are libmysqlcppconn.so
and libmysqlcppconn.dylib
. It is best to specify full paths to these libraries which are found at ${mysql-concpp_DIR}/lib64
.${mysql-concpp_DIR}/lib64/vs14
. The import library name is mysqlcppconnx.lib
(libmysqlcppconn.lib
for the classic library).We do not try to cover manual build configuration for static linking here. It requires careful listing of all dependencies, defining certain macros etc. If you need to do that your best bet is to look into mysql-concpp-config.cmake
file at the root of the connector install location and try to understand what it does in your particular scenario.