Using MySQL Connector/C++ with Visual Studio 2010
MySQL Connector/C++ is one of the latest connectors for MySQL, developed by Sun Microsystems. The MySQL connector for C++ provides an object-oriented application programming interface (API) and a database driver for connecting C++ applications to the MySQL Server.
When we want to build a database application with C++ using Visual Studio 2010, we may use the MySQL Connector/C++, but sometimes we may encounter different errors, either compile or runtime error. After tried many times, I finally made my C++ program successfully connect to MySQL database with Visual Studio 2010. Here I share what I have done..
- Visual Studio 2010
- MySQL Server 5.1.x
- MySQL Connector/C++ 1.1.0 (don't use MySQL Connector/C++ 1.0.5)
Note : MySQL Connector/C++ 1.1.0 needs boost libraries to work correctly, while MySQL Connector/C++ 1.0.5 doesn't need boost, but it causes runtime error when reading data from database. So we choose MySQL Connector/C++ 1.1.0 here .
1. Download all the necessaries files and libraries. We assume you have already installed Visual Studio 2010. For MySQL Server, you can download the msi installer and install it . Also you can download the MySQL Connector/C++ 1.1.0 msi file and install it.
2. Then what you need to do is to open Visual Studio 2010 and create a new C++ Win32 Console project. Then you can create a C++ file and copy the the example code from the Developing Database Applications Using MySQL Connector/C++.
3. Now you need to configure the project properties, Go to Project menu and open the properties window, you need to modify some places:
- VC++ Directories->Include Directories->Add the path of your MySQL Connector/C++ include folder
- VC++ Directories->Include Directories->Add the path of your MySQL Connector/C++ include/cppconn folder
- VC++ Directories->Include Directories->Add the path of your boost library folder (for me its (D:\Projects\C++ Projects\boost_1_49_0)
- VC++ Directories->Library Directories->Add the path of your MySQL Connector/C++ lib/opt folder
- Linker->Input->Additional Dependencies->Add "mysqlcppconn.lib"
4. Then you should change respective database settings in the source file such as host, username and password and database name.
5. Now build the solution and it should be built successfully.
Some errors you may encounter during the build process are:
- error C1083: Cannot open include file: 'sqlstring.h': No such file or directory d:\projects\c++ projects\lib\mysql_vs2010\include\cppconn\warning.h-->Solution : You need to download a MySQL Connector/C++ zip version from MySQL website because MySQL Connector/C++ msi version missed a header file named "sqlstring.h", you need copy the "sqlstring.h" file to the installed MySQL Connector/C++ include/cppconn folder.
- error C2371: 'int8_t' : redefinition; different basic types d:\projects\c++ projects\lib\mysql_vs2010\include\cppconn\config.h--> Solution : You can comment the line typedef __int8 int8_t; in MySQL Connector/C++ include\cppconn\config.h file
Error : unresolved external symbol __imp__get_driver_instance. -->Solution : This problem happens because the linker cannot find the mysqlcppconn.lib. You should do this "Project->[You project name] properties->Linker->Input->Additional Dependencies->Add mysqlcppconn.lib"
If you still have problems to make the MySQL Connector/C++ work with Visual Studio 2010. We can discuss together. Please comment below or email me : email@example.com. If you find any mistakes I made here, please feel free to let me know.