22.5.3. 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.


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

Static library

The MySQL Connector/C++ static library file is mysqlcppconn-static.lib. This needs to be statically linked with your application. You also need to 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 MySQL Connector/C++ dynamic library file is mysqlcppconn.dll. To build your client application, you must 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 MySQL 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 want to build your application to use the static or dynamic library.

  1. Select File, New, Project from the main menu.

    Figure 22.62. Creating a New Project

    Creating a new project
  2. 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, and then click OK, to move to the Win32 Application Wizard.

    Figure 22.63. The New Project Dialog Box

    The New Project dialog box
  3. In the Win32 Application Wizard, click Application Settings and ensure the defaults are selected. The radio button Console application, and the checkbox Precompiled headers will be selected. Click Finish to close the wizard.

    Figure 22.64. The Win32 Application Wizard

    The Win32 Application Wizard
  4. From the drop down list box on the toolbar, change from the default Debug build to the Release build.

    Figure 22.65. Selecting the Release Build

    Selecting the Release build
  5. From the main menu select Project, Properties. This can also be accessed using the hot key ALT + F7.

    Figure 22.66. Selecting Project Properties from the Main Menu

    Selecting Project Properties from the
              main menu
  6. Under Configuration Properties, open the tree view.

  7. Select C++, General in the tree view.

    Figure 22.67. Setting Properties

    Setting properties
  8. You now need to ensure that Visual Studio can find the MySQL include directory. This directory includes header files that can optionally be installed when installing MySQL Server.

    Figure 22.68. MySQL Include Directory

    MySQL include directory
  9. Then in the Additional Include Directories text field, add the MySQL include/ directory.

    Figure 22.69. Select Directory Dialog

    Select Directory dialog
  10. You will also need to set the location of additional libraries that Visual Studio will need to build the application. These are located in the MySQL lib/opt directory, a sub-directory of the MySQL Server installation directory.

    Figure 22.70. Typical Contents of MySQL lib/opt Directory

    Typical contents of MySQL lib/opt
  11. In the tree view open Linker, General, Additional Library Directories.

    Figure 22.71. Additional Library Directories

    Additional Library Directories
  12. Add the lib/opt directory into the Additional Library Directories text field. This enables the library file libmysql.lib to be found.

    Figure 22.72. Additional Library Directories Dialog

    Additional Library Directories

The remaining steps depend on whether you are building an application to use the MySQL 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:

  1. Then open Linker, Input, Additional Dependencies.

    Figure 22.73. 

  2. Enter mysqlcppconn-static.lib and libmysql.lib.

    Figure 22.74. Adding Additional Dependencies

    Adding additional dependencies
  3. 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, you must ensure that function prototypes are compatible with this. In this case CPPCONN_PUBLIC_FUNC needs to be defined 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 22.75. Setting the CPPCONN_PUBLIC_FUNC Define


    Make sure you enter CPPCONN_PUBLIC_FUNC= and not CPPCONN_PUBLIC_FUNC, as it needs to be defined as an empty string.

If building an application to use the MySQL Connector/C++ dynamically linked library carry out these steps:

  1. Under Linker, Input, add mysqlcppconn.lib into the Additional Dependencies text field.

  2. The application will need to access the MySQL Connector/C++ Dynamic Linked Library at run time. Therefore, mysqlcppconn.dll needs to be in the same directory as the application executable, or somewhere on the system's path.

    Copy mysqlcppconn.dll to the same directory as the application. Alternatively, extend the PATH environment variable using SET PATH=%PATH%;C:\path\to\cpp. Alternatively, you can copy mysqlcppconn.dll to the Windows installation Directory, typically c:\windows.

Copyright © 2010-2022 Platon Technologies, s.r.o.           Home | Man pages | tLDP | Documents | Utilities | About
Design by styleshout