MySQL Connector/C++ Building Windows Applications with Microsoft Visual Studio
MySQL Connector/C++ is available as a static or dynamic library to use with your application. This section looks at how to link the library to your application.Note
To avoid potential crashes the build configuration of MariaDB Connector/C++ should match the build configuration of the application using it. For example, do not use the release build of MariaDB Connector/C++ with a debug build of the client application.
Static library
The MariaDB Connector/C++ static library file is mysqlcppconn-static.lib
. You link this library statically with your application. Also link against the files libmysql.dll
and libmysql.lib
. Once linking has been successfully completed, the application will require access to libmysql.dll
at run time.
Dynamic library
The MariaDB Connector/C++ dynamic library file is mysqlcppconn.dll
. To build your client application, link it with the file mysqlcppconn.lib
. At run time, the application will require access to the files mysqlcppconn.dll
and libmysql.dll
.
Building a MariaDB Connector/C++ application with Microsoft Visual Studio
Initially, the procedure for building an application to use either the static or dynamic library is the same. You then carry out some additional steps depending on whether you are building your application to use the static or dynamic library.
- Select File, New, Project from the main menu.
Figure 20.62. Creating a New Project
- In the wizard, select Visual C++, Win32. From Visual Studio Installed Templates, select the application type Win32 Console Application. Enter a name for the application, then click OK, to move to the Win32 Application Wizard.
Figure 20.63. The New Project Dialog Box
- In the Win32 Application Wizard, click Application Settings and ensure the defaults are selected. The radio button Console application and the check box Precompiled headers are selected. Click Finish to close the wizard.
Figure 20.64. The Win32 Application Wizard
- From the drop down list box on the toolbar, change from the default Debug build to the Release build.
Figure 20.65. Selecting the Release Build
- From the main menu select Project, Properties. This can also be accessed using the hot key ALT + F7.
Figure 20.66. Selecting Project Properties from the Main Menu
- Under Configuration Properties, open the tree view.
- Select C++, General in the tree view.
Figure 20.67. Setting Properties
- Now ensure that Visual Studio can find the MariaDB include directory. This directory includes header files that can optionally be installed when installing MariaDB Server.
Figure 20.68. MariaDB Include Directory
- In the Additional Include Directories text field, add the MariaDB
include/
directory.
Figure 20.69. Select Directory Dialog
- Also set the location of additional libraries that Visual Studio needs to build the application. These are located in the MariaDB
lib/opt
directory, a subdirectory of the MariaDB Server installation directory.
Figure 20.70. Typical Contents of MariaDB lib/opt Directory
- In the tree view, open Linker, General, Additional Library Directories.
Figure 20.71. Additional Library Directories
- Add the
lib/opt
directory into the Additional Library Directories text field. This enables the library filelibmysql.lib
to be found.
Figure 20.72. Additional Library Directories Dialog
The remaining steps depend on whether you are building an application to use the MariaDB Connector/C++ static or dynamic library. If you are building your application to use the dynamic library go here. If you are building your application to use the static library, carry out the following steps:
- Then open Linker, Input, Additional Dependencies.
Figure 20.73.
- Enter
mysqlcppconn-static.lib
andlibmysql.lib
.
Figure 20.74. Adding Additional Dependencies
- By default
CPPCONN_PUBLIC_FUNC
is defined to declare functions to be compatible with an application that calls a DLL. If building an application to call the static library, ensure that function prototypes are compatible with this. In this case, defineCPPCONN_PUBLIC_FUNC
to be an empty string, so that functions are declared with the correct prototype.
In the Project, Properties tree view, under C++, Preprocessor, enter
CPPCONN_PUBLIC_FUNC=
into the Preprocessor Definitions text field.Figure 20.75. Setting the CPPCONN_PUBLIC_FUNC Define
Note
Make sure you enter
CPPCONN_PUBLIC_FUNC=
and notCPPCONN_PUBLIC_FUNC
, so that it is defined as an empty string.
If building an application to use the MariaDB Connector/C++ dynamically linked library carry out these steps:
- Under Linker, Input, add
mysqlcppconn.lib
into the Additional Dependencies text field. mysqlcppconn.dll
must be in the same directory as the application executable, or somewhere on the system's path, so that the application can access the MariaDB Connector/C++ Dynamic Linked Library at runtime.
Copy
mysqlcppconn.dll
to the same directory as the application. Alternatively, extend thePATH
environment variable usingSET PATH=%PATH%;C:\path\to\cpp
. Alternatively, you can copymysqlcppconn.dll
to the Windows installation Directory, typicallyc:\windows
.