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
. 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 MySQL 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 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 are building your application to use the static or dynamic library.
Select File, New, Project from the main menu.
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.
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.
From the drop down list box on the toolbar, change from the default Debug build to the Release build.
From the main menu select Project, Properties. This can also be accessed using the hot key ALT + F7.
Under Configuration Properties, open the tree view.
Select C++, General in the tree view.
Now ensure that Visual Studio can find the MySQL include directory. This directory includes header files that can optionally be installed when installing MySQL Server.
In the Additional Include Directories
text field, add the MySQL include/
directory.
Also set the location of additional libraries that Visual
Studio needs to build the application. These are located in
the MySQL lib/opt
directory, a
subdirectory of the MySQL Server installation directory.
In the tree view, open Linker, General, Additional Library Directories.
Add the lib/opt
directory into the
Additional Library Directories text
field. This enables the library file
libmysql.lib
to be found.
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:
Then open Linker, Input, Additional Dependencies.
Enter mysqlcppconn-static.lib
and
libmysql.lib
.
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, define
CPPCONN_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.
Make sure you enter CPPCONN_PUBLIC_FUNC=
and not CPPCONN_PUBLIC_FUNC
, so that it
is defined as an empty string.
If building an application to use the MySQL 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 MySQL Connector/C++
Dynamic Linked Library at runtime.
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
.
User Comments
The boost libraries are now required. Install the boost libraries (from http://www.boostpro.com/download/) and remember to update 'Additional Include Diretories' to point to where you installed the boost libraries.
sqlstring.h is not included in mysql-connector-c++-1.1.0-win32.msi. It can be found at http://mysql-connector-cplus-pplus-p.sourcearchive.com/documentation/1.0.6~r768/sqlstring_8h-source.html (cut and paste from the web page) and save as sqlstring.h in the same directory as the other headers.
If using 'debug' then the directories for the linker 'Additional Library Directories' needs to use 'debug' and not 'opt'. The dlls for the debug also need to come from 'lib\debug' and not 'lib\opt'(both the connector and libmysql).
"and save as sqlstring.h in the same directory as the other headers" is not very clear for a beginner.
The C++ Connector is currently not compatible with visual studio 2010. You will get an error saying something like "redefinition int8_t"
This is because in the config.h file there is a #typedef for int8_t however in the standard library for vs there is a file named stdint.h which has a different #typedef for int8_h.
If you add the following Preprocessor Definition it will take care of the compiler error "redefinition int8_t" in VS 2010
HAVE_INT8_T=1
You also need to add \MySQL\Connector C++ 1.1.0\lib\opt to the Additional Library Directories so mysqlcppconn.lib and/or mysqlcppconn-static.lib can be found.
Posted by Dan Bray on October 31 2011 5:43pm [Delete] [Edit]
The C++ Connector is currently not compatible with visual studio 2010. You will get an error saying something like "redefinition int8_t"
This is because in the config.h file there is a #typedef for int8_t however in the standard library for vs there is a file named stdint.h which has a different #typedef for int8_h.
To Dan Bray: If you have a redefinition issue, you may be able to correct it by editing config.h in the following manner, instead of:
#typedef int8_t
replace with:
#ifndef int8_t
#typedef int8_t
#endif
If the compiler has not already defined int8_t, then it'll go ahead and define it, if it's already defined, it'll bypass this definition. Be sure that you #include <stdint.h> before config.h as well so it can get defined in stdint.h first. I have VC 2010 express, so I'll try this myself, if it still doesn't work, I'll post the solution when I find it. Good luck.
I ran into a problem compiling the CPP Connector with Visual Studio 2010 this morning (version 1.1.1 and MySQL 5.5.28). There were many compiler errors related to conflicts with the stdint.h header that is provided with Visual Studio 2010. To resolve the issue, and get the compile to work, I did the following:
1) Comment out all code in file config.h
2) Modify native_resultset_wrapper.h, native_connection_wrapper.h, and native_statement_wrapper.h as follows:
//#include <config.h> // file is now empty so remove the include
#include <boost/cstdint.hpp> // CPP Conenctor is already dependent on boost and boost does it right
#include <boost/noncopyable.hpp>
//#ifdef HAVE_STDINT_H // no longer needed
//#include <stdint.h> // no longer needed
//#endif // no longer needed
Add your own comment.