Connectors and APIs Manual  /  ...  /  Building Connector/C++ Applications: General Considerations

3.5.1 Building Connector/C++ Applications: General Considerations

This section discusses general considerations to keep in mind when building Connector/C++ applications. For information that applies to particular platforms, see the section that applies to your platform in Section 3.5.2, “Building Connector/C++ Applications: Platform-Specific Considerations”.

Commands shown here are as given from the command line (for example, as invoked from a Makefile). The commands apply to any platform that supports make and command-line build tools such as g++, cc, or clang, but may need adjustment for your build environment.

Build Tools and Configuration Settings

It is important that the tools you use to build your Connector/C++ applications are compatible with the tools used to build Connector/C++ itself. Ideally, build your applications with the same tools that were used to build the Connector/C++ binaries.

To avoid issues, ensure that these factors are the same for your applications and Connector/C++ itself:

  • Compiler version.

  • Runtime library.

  • Runtime linker configuration settings.

To avoid potential crashes, the build configuration of Connector/C++ should match the build configuration of the application using it. For example, do not use a release build of Connector/C++ with a debug build of the client application.

To use a different compiler version, release configuration, or runtime library, first build Connector/C++ from source using your desired settings (see Section 3.4, “Installing Connector/C++ from Source”), then build your applications using those same settings.

Connector/C++ binary distributions include an INFO_BIN file that describes the environment and configuration options used to build the distribution. If you installed Connector/C++ from a binary distribution and experience build-related issues on a platform, it may help to check the settings that were used to build the distribution on that platform. Binary distributions also include an INFO_SRC file that provides information about the product version and the source repository from which the distribution was produced. (Prior to Connector/C++ 8.0.14, look for BUILDINFO.txt rather than INFO_BIN and INFO_SRC.)

C++11 Support

X DevAPI uses C++11 language features. To compile Connector/C++ applications that use X DevAPI, enable C++11 support in the compiler using the -std=c++11 option. This option is not needed for applications that use X DevAPI for C (which is a plain C API) or the legacy JDBC API (which is based on plain C++), unless the application code uses C++11.

Boost Header Files

The Boost header files are needed under these circumstances:

  • To compile Connector/C++ applications that use the legacy JDBC API.

  • Prior to Connector/C++ 8.0.16, on Unix and Unix-like platforms for applications that use X DevAPI or X DevAPI for C, if you build using gcc and the version of the C++ standard library on your system does not implement the UTF8 converter (codecvt_utf8).

If the Boost header files are needed, Boost 1.59.0 or newer must be installed, and the location of the headers must be added to the include path. To obtain Boost and its installation instructions, visit the official Boost site.

Link Libraries

Building Connector/C++ using OpenSSL makes the connector library dependent on OpenSSL dynamic libraries. In that case:

  • When linking an application to Connector/C++ dynamically, this dependency is relevant only at runtime.

  • When linking an application to Connector/C++ statically, link to the OpenSSL libraries as well. On Linux, this means adding -lssl -lcrypto explicitly to the compile/link command. On Windows, this is handled automatically.

On Windows, link to the dynamic version of the C++ Runtime Library.

Runtime Libraries

X DevAPI for C applications need libstdc++ at runtime. Depending on your platform or build tools, a different library may apply. For example, the library is libc++ on macOS; see Section 3.5.2.2, “macOS Notes”.

If an application is built using dynamic link libraries, those libraries must be present not just on the build host, but on target hosts where the application runs. The dynamic linker must be properly configured to find those libraries and their runtime dependencies, as well as to find Connector/C++ libraries and their runtime dependencies.

Connector/C++ libraries built by Oracle depend on the OpenSSL libraries. The latter must be installed on the system in order to run code that links against Connector/C++ libraries. Another option is to put the OpenSSL libraries in the same location as Connector/C++ In this case the dynamic linker should find them next to the connector library. See also Section 3.5.2.1, “Windows Notes”, and Section 3.5.2.2, “macOS Notes”.

It is possible to build Connector/C++ with an SSL library other than OpenSSL. Use the WITH_SSL CMake option to specify which SSL library to use (see Section 3.4.4, “Connector/C++ Source-Configuration Options”). For applications linked against Connector/C++ libraries that are not built using OpenSSL, the OpenSSL libraries are not required at runtime.

Connector/C++ Header Files

The API an application uses determines which Connector/C++ header files it should include. The following include directives work under the assumption that the include path contains $MYSQL_CPPCONN_DIR/include, where $MYSQL_CPPCONN_DIR is the Connector/C++ installation location. Pass an -I $MYSQL_CPPCONN_DIR/include option on the compiler invocation command to ensure this.

  • For applications that use X DevAPI:

    #include <mysqlx/xdevapi.h>
  • For applications that use X DevAPI for C:

    #include <mysqlx/xapi.h>
  • For applications that use the legacy JDBC API:

    • As of Connector/C++ 8.0.16, a single #include directive suffices:

      #include <mysql/jdbc.h>
    • Prior to Connector/C++ 8.0.16, use this set of #include directives:

      #include <jdbc/mysql_driver.h>
      #include <jdbc/mysql_connection.h>
      #include <jdbc/cppconn/*.h>
    • Legacy code that uses Connector/C++ 1.1 has #include directives of this form:

      #include <mysql_driver.h>
      #include <mysql_connection.h>
      #include <cppconn/*.h>

      To build such code with Connector/C++ 8.0 without modifying it, add $MYSQL_CPPCONN_DIR/include/jdbc to the include path.

To compile code that you intend to link statically against Connector/C++, define a macro that adjusts API declarations in the header files for usage with the static library. For details, see Using the Connector/C++ Static Library.

Using the Connector/C++ Dynamic Library

The Connector/C++ dynamic library name depends on the platform. These libraries implement X DevAPI and X DevAPI for C, where A in the library name represents the ABI version:

  • libmysqlcppconn8.so.A (Unix)

  • libmysqlcppconn8.A.dylib (macOS)

  • mysqlcppconn8-A-vsNN.dll, with import library vsNN/mysqlcppconn8.lib (Windows)

For the legacy JDBC API, the dynamic libraries are named as follows:

  • libmysqlcppconn.so.7 (Unix)

  • libmysqlcppconn.7.dylib (macOS)

  • mysqlcppconn-7-vsNN.dll, with import library vsNN/mysqlcppconn-static.lib (Windows)

On Windows, the vsNN value in library names depends on the MSVC compiler version used to build the libraries (for example, vs14 for MSVC 2015). This convention enables using libraries built with different versions of MSVC on the same system. For details, see Section 3.5.2.1, “Windows Notes”.

To build code that uses X DevAPI or X DevAPI for C, add -lmysqlcppconn8 to the linker options. To build code that uses the legacy JDBC API, add -lmysqlcppconn. You must also indicate whether to use the 64-bit or 32-bit libraries by specifying the appropriate library directory. Use an -L linker option to specify $MYSQL_CONCPP_DIR/lib64 (64-bit libraries) or $MYSQL_CONCPP_DIR/lib (32-bit libraries), where $MYSQL_CPPCONN_DIR is the Connector/C++ installation location.

To build a Connector/C++ application that uses X DevAPI, has sources in app.cc, and links dynamically to the connector library, the Makefile might look like this:

MYSQL_CONCPP_DIR = Connector/C++ installation location
CPPFLAGS = -I $(MYSQL_CONCPP_DIR)/include -L $(MYSQL_CONCPP_DIR)/lib64
LDLIBS = -lmysqlcppconn8
CXXFLAGS = -std=c++11
app : app.cc

With that Makefile, the command make app generates the following compiler invocation:

g++ -std=c++11 -I .../include -L .../lib64 app.cc -lmysqlcppconn8 -o app

To build a plain C application that uses X DevAPI for C, has sources in app.c, and links dynamically to the connector library, the Makefile might look like this:

MYSQL_CONCPP_DIR = Connector/C++ installation location
CPPFLAGS = -I $(MYSQL_CONCPP_DIR)/include -L $(MYSQL_CONCPP_DIR)/lib64
LDLIBS = -lmysqlcppconn8
app : app.c

With that Makefile, the command make app generates the following compiler invocation:

cc -I .../include -L .../lib64 app.c -lmysqlcppconn8 -o app

Typically, X DevAPI for C applications are written in plain C. However, if application code does use C++11, the compiler command should also specify the -std=c++11 option:

CXXFLAGS = -std=c++11
Note

The resulting code, even though it is compiled as plain C, depends on the C++ runtime (typically libstdc++, though this may differ depending on platform or build tools; see Runtime Libraries).

To build a plain C++ application that uses the legacy JDBC API, has sources in app.c, and links dynamically to the connector library, the Makefile might look like this:

MYSQL_CONCPP_DIR = Connector/C++ installation location
CPPFLAGS = -I $(MYSQL_CONCPP_DIR)/include -L $(MYSQL_CONCPP_DIR)/lib64
LDLIBS = -lmysqlcppconn
app : app.c

The library option in this case is -lmysqlcppcon, rather than -lmysqlcppcon8 as for an X DevAPI or X DevAPI for C application.

With that Makefile, the command make app generates the following compiler invocation:

cc -I .../include -L .../lib64 app.c -lmysqlcppconn -o app

Typically, applications that use the legacy JDBC API are written in plain C++. However, if application code does use C++11, the compiler command should also specify the -std=c++11 option:

CXXFLAGS = -std=c++11
Note

When running an application that uses the Connector/C++ dynamic library, the library and its runtime dependencies must be found by the dynamic linker. See Runtime Libraries.

Using the Connector/C++ Static Library

It is possible to link your application with the Connector/C++ static library. This way there is no runtime dependency on the connector, and the resulting binary can run on systems where Connector/C++ is not installed.

Note

Even when linking statically, the resulting code still depends on all runtime dependencies of the Connector/C++ library. For example, if Connector/C++ is built using OpenSSL, the code has a runtime dependency on the OpenSSL libraries. See Runtime Libraries.

The Connector/C++ static library name depends on the platform. These libraries implement X DevAPI and X DevAPI for C:

  • libmysqlcppconn8-static.a (Unix, macOS)

  • vsNN/mysqlcppconn8-static.lib (Windows)

For the legacy JDBC API, the static libraries are named as follows:

  • libmysqlcppconn-static.a (Unix, macOS)

  • vsNN/mysqlcppconn-static.lib (Windows)

On Windows, the vsNN value in library names depends on the MSVC compiler version used to build the libraries (for example, vs14 for MSVC 2015). This convention enables using libraries built with different versions of MSVC on the same system. For details, see Section 3.5.2.1, “Windows Notes”.

To compile code that you intend to link statically against Connector/C++, define a macro that adjusts API declarations in the header files for usage with the static library. One way to define the macro is by passing a -D option on the compiler invocation command:

  • For applications that use X DevAPI or X DevAPI for C, define the STATIC_CONCPP macro. All that matters is that you define it; the value does not matter. For example: -DSTATIC_CONCPP

  • For applications that use the legacy JDBC API, define the CPPCONN_PUBLIC_FUNC macro as an empty string. To ensure this, define the macro as CPPCONN_PUBLIC_FUNC=, not as CPPCONN_PUBLIC_FUNC. For example: -DCPPCONN_PUBLIC_FUNC=

To build a Connector/C++ application that uses X DevAPI, has sources in app.cc, and links statically to the connector library, the Makefile might look like this:

MYSQL_CONCPP_DIR = Connector/C++ installation location
CPPFLAGS = -DSTATIC_CONCPP -I $(MYSQL_CONCPP_DIR)/include
LDLIBS = $(MYSQL_CONCPP_DIR)/lib64/libmysqlcppconn8-static.a -lssl -lcrypto -lpthread
CXXFLAGS = -std=c++11
app : app.cc

With that Makefile, the command make app generates the following compiler invocation:

g++ -std=c++11 -DSTATIC_CONCPP -I .../include app.cc
  .../lib64/libmysqlcppconn8-static.a -lssl -lcrypto -lpthread -o app
Note

To avoid having the linker report unresolved symbols, the compile line must include the OpenSSL libraries and the pthread library on which Connector/C++ code depends.

OpenSSL libraries are not needed if Connector/C++ is built without them, but Connector/C++ distributions built by Oracle do depend on OpenSSL.

The exact list of libraries required by Connector/C++ library depends on the platform. For example, on Solaris, the socket, rt, and nsl libraries might be needed.

To build a plain C application that uses X DevAPI for C, has sources in app.c, and links statically to the connector library, the Makefile might look like this:

MYSQL_CONCPP_DIR = Connector/C++ installation location
CPPFLAGS = -DSTATIC_CONCPP -I $(MYSQL_CONCPP_DIR)/include
LDLIBS = $(MYSQL_CONCPP_DIR)/lib64/libmysqlcppconn8-static.a -lssl -lcrypto -lpthread
app : app.c

With that Makefile, the command make app generates the following compiler invocation:

cc -DSTATIC_CONCPP -I .../include app.c
  .../lib64/libmysqlcppconn8-static.a -lssl -lcrypto -lpthread -o app

If the application code uses C++11, the compiler command should also specify the -std=c++11 option:

CXXFLAGS = -std=c++11

To build a plain C application that uses the legacy JDBC API, has sources in app.c, and links statically to the connector library, the Makefile might look like this:

MYSQL_CONCPP_DIR = Connector/C++ installation location
CPPFLAGS = -DCPPCONN_PUBLIC_FUNC= -I $(MYSQL_CONCPP_DIR)/include
LDLIBS = $(MYSQL_CONCPP_DIR)/lib64/libmysqlcppconn-static.a -lssl -lcrypto -lpthread
app : app.c

The library option in this case names libmysqlcppcon-static.a, rather than libmysqlcppcon8-static.a as for an X DevAPI or X DevAPI for C application.

With that Makefile, the command make app generates the following compiler invocation:

cc -std=c++11 --DCPPCONN_PUBLIC_FUNC= -I .../include app.c
  .../lib64/libmysqlcppconn-static.a -lssl -lcrypto -lpthread -o app

If the application code uses C++11, the compiler command should also specify the -std=c++11 option:

CXXFLAGS = -std=c++11

When building plain C code, it is important to take care of connector's dependency on the C++ runtime, which is introduced by the connector library even though the code that uses it is plain C:

  • One approach is to ensure that a C++ linker is used to build the final code. This approach is taken by the Makefile shown here:

    MYSQL_CONCPP_DIR = Connector/C++ installation location
    CPPFLAGS = -DSTATIC_CONCPP -I $(MYSQL_CONCPP_DIR)/include
    LDLIBS = $(MYSQL_CONCPP_DIR)/lib64/libmysqlcppconn8-static.a -lssl -lcrypto -lpthread
    LINK.o = $(LINK.cc) # use C++ linker
    app : app.o

    With that Makefile, the build process has two steps: first the application source in app.c is compiled using a plain C compiler to produce app.o, then the final executable (app) is linked using the C++ linker, which takes care of the dependency on the C++ runtime:

    cc -DSTATIC_CONCPP -I .../include -c -o app.o app.c
    g++ -DSTATIC_CONCPP -I .../include app.o
      .../libmysqlcppconn8-static.a -lssl -lcrypto -lpthread -o app
  • Another approach is to use a plain C compiler and linker, but add the libstdc++ C++ runtime library as an explicit option to the linker. This approach is taken by the Makefile shown here:

    MYSQL_CONCPP_DIR = Connector/C++ installation location
    CPPFLAGS = -DSTATIC_CONCPP -I $(MYSQL_CONCPP_DIR)/include
    LDLIBS = $(MYSQL_CONCPP_DIR)/lib64/libmysqlcppconn8-static.a -lssl -lcrypto -lpthread -lstdc++
    app : app.c

    With that Makefile, the compiler is invoked as follows:

    cc -DSTATIC_CONCPP -I .../include app.c
      .../libmysqlcppconn8-static.a -lssl -lcrypto -lpthread -lstdc++ -o app
Note

Even if the application that uses Connector/C++ is written in plain C, the final executable depends on the C++ runtime which must be installed on the target computer on which the application will run.