Just Code it!

This post is a tutorial that will explain hot to write a simple C++ code that can be considered the starting point for any other program you intend to write in C++ with the intention to use a MySQL Database.

In order to connect to MySQL from C++ we will use some libraries specifically designed for this purpose.

Despite the concept seems simple, there are a series of gotchas and configurations that can put off the task many people, increasing the amount of work needed and not necessarily forecasted.

This tutorial will reach the goal starting essentially from scratch, where the environment needs to be completely configured… even without Visual Studio!

While writing this tutorial, I decided to split it in multiple sub-posts because of the amount of information I ended up collecting. Doing this will remove some of the noise around the main topic and will allow to follow the post and the specific sections even for people with different requirements that have already available some of the systems I am going to describe.

As a reference, these are the additional posts:


Enough said, let’s start the journey! See you at the end of this. Happy reading.

mysql-connector (1.1.4) – Download

First things first… let’s immediately download what we are really interested in, the C++ libraries (source) that will allow us to connect and interact with a MySQL database.

Open the browser to the MySQL website and navigate to the page with the information about the connector:


Select the “Source Code” option from the drop-down menu:

Download the generic Windows version:

In the following page select your preferred download option or just download it using the link at the bottom of the page:

Let’s remember the warning reported on the website:

One problem that can occur is when the tools you use to build your application are not compatible with the tools used to build the binary versions of MySQL Connector/C++. Ideally, you need to build your application with the same tools that were used to build the MySQL Connector/C++ binaries. 


Create a folder, for example “C:\libs\” and extract the archive in there.

The structure should look like this:

In order to build the source code for the connector libraries we need to download CMake.

It’s time to switch to the first sub-post if you need to perform this step and install CMake (CMake Installation and usage (Command Prompt Console and GUI)).

Jump to this post and see you here once you are ready to continue (I would suggest you to follow just the initial part of the tutorial, limited to installing it. The remainder will be useful later on and will probably not work on your system if you don’t have everything else already configured): CMake Installation and usage

Back from CMake Installation

I will assume that you have CMake installed on your system now.

mysql-connector 1.1.4 – Build – First Potential Problem (Missing Visual Studio)

If we try to run the CMake command to generate the Visual Studio solution from the mysql-connector code we could potentially immediately receive an error.
Let’s try this using the Command line:

C:\libs\mysql-connector-c++-1.1.4>cmake -G “Visual Studio 10”

If you don’t have the C/C++ compiler then you will see this error:

Pay particular attention to the lines:

If that’s the case, what you need to do is installing Visual Studio.

Visual Studio (C++ 2010 Express)

Let’s download Visual Studio (Visual C++ 2010 Express is ok for us)
Go to the website:
and find the Install Now link:
Install it executing the installation file (vc_web.exe) downloaded.
Then try again!

mysql-connector (1.1.4) – Build – Second Potential Problem (Missing Boost Libraries)

If we try again now:
C:\libs\mysql-connector-c++-1.1.4>cmake -G “Visual Studio 10”
This time it should go further and complain about the (potential) next problem:


The main issue is now:

The problem now is that in order to build this code you need to have installed the BOOST libraries.
If that’s the problem you see as well, then you can happily jump to the next sub-post where is explained in detail how to install and configure boost libraries: Boost Libraries Installation
See you when you are ready to proceed!

Back from Boost Libraries Installation

You should now have a system with CMake, Visual Studio and Boost Libraries installed and configured. Time to continue!

mysql-connector (1.1.4) – Build – Third Potential Problem (MySQL not installed)

Go back into the connector folder and execute again:
C:\libs\mysql-connector-c++-1.1.4>cmake -G “Visual Studio 10”


If you decided at any point to move the folder after executing cmake the first time you will potentially see an error:

To quickly fix this, just remove the file “CMakeCache.txt” and run cmake again.

If you don’t have MySQL installed on your system, then you will probably see this error:

The main indicator for this problem is the line:
If that’s what you see as well… well, you know what’s coming. Yep, another sub-post: MySQL Service, Workspace, Users and Tables
Jump to this post and you can read for now just the initial part that covers the download and installation of MySQL.
The rest of that post will show how to create a database, user and table that we will here, but you can go back to that section later on.

Back from MySQL installation

If everything went as planned until this point you should have CMake, Boost libraries,Visual Studio and MySQL installed and ready to rock.

mysql-connector (1.1.4) – Build – Creating the Visual Studio Solution

Now we can go back into our connector folder and try once again to recreate our Visual Studio Solution.
Yhou can follow this step in the CMake tutorial if you want (where it is explained how to obtain the same result using the command line or the GUI).
This is an extract of it, where I will show only the command line option.
If you run the same command again:
C:\libs\mysql-connector-c++-1.1.4>cmake -G “Visual Studio 10”

You should finally succeed in this step, obtaining:
Now, if everything went as planned, you should be able to see a solution file (and other files as well) into your connector’s folder:
Now we can finally open the created solution (MYSQLCPPCONN.sln) with Visual Studio!
Simply double-click on:

Build MySqlCppConn.sln – Debug

In Visual studio, set the configuration to DEBUG and try to Build.
What should happen is that you will see a series of errors:

Open the properties for the project “mysqlcppconn“. Right-click on it and select “Properties”:

We can do this using different techniques, but I want to use one (even if it is not my preferred one) just for a change :).


Make sure to be in the configuration “Debug” and go in the section:

Common Properties -> Linker -> Input


Open the “Additional Dependencies” drop-down list and click on “<Edit…>”:

Substitute the entry:

  • C:\Program Files\MySQL\MySQL Server 5.6\lib\mysqlclient.lib
  •  C:\Program Files\MySQL\MySQL Server 5.6\lib\debug\mysqlclient.lib

If you build now you could receive another error:

Open the project properties for the project “mysqlcppconn-static

Go in the section:
Common Properties -> Librarian -> Command Line
and change the additional options from:
  • C:/Program Files/MySQL/MySQL Server 5.6/lib/mysqlclient.lib
  •  “C:/Program Files/MySQL/MySQL Server 5.6/lib/debug/mysqlclient.lib”
including the quotes
Build again and everything should be ok now!

Build MySqlCppConn.sln – Release

For the release build the only fix I had to do was only the “Additional Options” section in the Librarian -> Command Line

section for the “mysqlcppconn-static” project (in Release configuration).

Add the quotes to:

  • C:/Program Files/MySQL/MySQL Server 5.6/lib/mysqlclient.lib
Also in this case you should receive a successful compilation:



If you open now the window explorer in the connector folder, in the sub-folder “driver”:
you should see the two build folders “Debug” and “Release”:
Inside these you should find at least the three files:
  • mysqlcppconn.dll
  • mysqlcppconn.lib
  • mysqlcppconn-static.lib
We are ready to use it finally!

Creating a DB

It is finally time to create a database in MySQL that we will use in C++.
You can refer back to the post already highlighted previously (MySQL installation and Usage) and continue to follow it from where you left it until the end.
In that tutorial I explain how to create a sample DB with a sample user that will have access rights and a sample table that we will be able access from code.
When that system is ready, is time to jump into the final step!

Coding – Create a new Visual Studio Solution

In Visual Studio create a new project (I selected a simple Win32 Console Application)
Right click on the source folder and add a new cpp file:
Let’s give it a simple name (the very original “main.cpp” :P):
You should see the new file added:
Before writing any code, let’s configure our environment setting up the folder and libraries that we need to use/reference in order to successfully build our app.

Coding – Configure the Environment

Open the properties window of the project and go to the settings page:
Configuration Properties -> VC++ Directories
And edit the entry:
Include Directories
Make sure you are in the “All Configurations” configuration:

Edit it adding the lines:



Edit then the Library Directories property, adding the line:


Edit then the property in:

Configuration Properties -> Linker -> Input -> Additional Dependencies

adding the line:


One last step you may need to do is copying the “libmysql.dll” and “copy “mysqlcppconn.lib” into your output folder.

Just open the properties to:

Configuration Properties -> Build Events -> Post-Build Event

Then edit “Command Line” and type in this (or the equivalent line that you need in order to copy the missing dll to the output folder):

copy “C:\Program Files\MySQL\MySQL Connector C 6.1.5\lib\libmysql.dll” “$(OutDir)”

copy “C:\libs\mysql-connector-c++-1.1.4\driver\$(Configuration)\mysqlcppconn.dll”  “$(OutDir)”

Remember to edit All Configurations:

Finally it’s time to code!

Coding – Code: Connecting to MySQL

Open the “main.cpp” and let’s put some code.

The first simple code will just test the library, connecting to the DB (our one) using the user that we previously set-up in MySQL Workbench.
Remember to configure correctly:
  • The database address (and port)
  • The User and Password for your user
  • The Database name (that contains the “contacts” table)
That’s the simplest code with some error handling:

If your run this code and you didn’t set-up correctly one of the used parameters, then you will see the error text (without crashing). If that’s the case, check your parameters and verify that you will see the “Connection to the DB established!” string.

Coding – Code: Connecting and using the DB

For the final part of this post we are going to see how to connect now to our DB and retrieve and print the data from our table!
The new lines are the ones that follow the connection established message and few extra includes.
I will omit any exception handling to keep the code short and simple.
This is the final code:

If you build and run the code, you should be finally happily able to see the rows that we entered in our table using MySQL Workbench!


That’s all!


Of course you can now play around with it inserting, editing, removing entries. For these functionalities and more advanced one I point you to the official website and in particular to the Developer Zone:
Please Link and comment if you found this interesting or useful.

As for other posts (see Boost Libraries Installation), this post is actually a sub-post I made while I was writing a larger post where I explain how to download and use MySQL connector C++ libraries (MySQL C++ with mysql-connector).

That post soon became pretty big due to the amount of stuff you need to configure in order to achieve the goal, therefore I decided to split it in smaller posts that on one side are useful on their own and on the other helps in keeping the main post they came from slightly smaller and easier to follow. Because of this I will keep these posts quite short.

If you are interested in this post as well, you can find it there:

For this example I will use MySQL Connector for C++ as a sample to show the usage of CMake to generate Visual Studio solutions.

Let’s start!


CMake (3.0.2) – Download and Install

In order to build the source code for the connector libraries we need to download CMake
Go to the CMake website:
and download it:
Select the Installer in this case:
Execute the installer. I selected the option to add it to the system PATH for an easy access:
Just go Next until the end and Install it.
CMake (3.0.2) – Test
To test that everything is ok with cmake, open a command console and go into the folder where we extracted the connector code and execute:



We should see this output:

We can generate the Visual studio project in two ways:

  • Using the GUI
  • Using the command line



CMake (3.0.2) – Usage from Command Line to generate the Visual Studio Solution
At this stage, if you are following the MySQL connector tutorial () you want probably to go back to follow the rest of the tutorial, where you will install first all the required libraries and systems that you need in order to successfully execute the next command.
If you did instead all the required step and everything is correctly configured or you want just to see an example of the usage of CMake for this purpose, then keep reading.

Now we can go back into our connector folder and try once again to recreate our Visual Studio Solution:
C:\libs\mysql-connector-c++-1.1.4>cmake -G “Visual Studio 10”



Now, if everything went as planned, you should be able to see a solution file (and other files as well) into your connector’s folder:

You can then open the solution and continue from there.
CMake (3.0.2) – Usage from GUI to generate the Visual Studio Solution
An alternative way to generate the Solution file is to use the CMake GUI.
Launch the cmake-gui from the start menu:
In the window that opens select “Browse Source…” button and locate the root of your project.
I will select the one where the code for MySQL Connector has been extracted:
Copy the same path for the binaries, then click on “Generate” (or Configure) and select the IDE you are interested in.
I will select Visual Studio 2010 for this example.
Click “Finish” when ready:
Everything should complete successfully (pay particular attention at the bottom window without being too much scared by the red entries in the main window):
If you try to click once again (or why not… a couple of times as well :P) on the “Generate” button, all the red should disappear from the main window:
Anyway, despite the colouring, you should still be able to see your new solution file generated and ready to be opened in Visual Studio.
Now, if were following the MySQL connector tutorial (MySQL C++ with mysql-connector) you can go back and follow the remaining part of that tutorial.


Boost Libraries Installation

January 25th, 2015 | Posted by riccardotramma in Boost | C++ | Download - (0 Comments)

This post is really a subpost I made while I was writing a larger post where I explain how to download and use MySQL connector C++ libraries (MySQL C++ with mysql-connector).

That post soon became pretty big due to the amount of stuff you need to configure in order to achieve the goal, therefore I decided to split it in smaller posts that one one side are useful on their own and on the other helps in keeping the main post they came from slightly smaller and easier to follow. Because of this I will keep these posts quite short.

If you are interested in this post as well, you can fint it there:

Let’s enter in the core of the topic now!


Boost Libraries (1.56.0) – Download
Let’s download them here:
Just click on the Download link:
Click then on the zip file:
Once downloaded extract it, for example in the same lib folder, so you should now have:
Boost Libraries (1.56.0) – Install
Enter into the boost_1_56_0 folder and execute the file “bootstrap.bat“:
The output should be: 
The next step, is to execute “.\b2”. We could use some parameter here, such as:
b2 install –prefix=PREFIX 

where PREFIX is a directory where you want Boost.Build to be installed.
Optionally, add PREFIX/bin to your PATH environment variable. 

We will be ok with the default setting, therefore we will just execute:

C:\libs\boost_1_56_0>b2 install
It will start to compile the code… it will take a bit and a loooong output.
The initial part will be something along this line:


Once this is done you should see a new folder on your drive: “C:\Boost\
This is how the situation should look like at this point:
That’s it for this part.
As mentioned this is a subpart I splitted from a larger topic.
If you want to continue the reading of the original topic that originates this continue here: MySQL C++ with mysql-connector
Thanks and share and comment if you find this useful or interesting.
Comment or Leave a Message

Please Comment or Leave a Message if this is what you are looking for or if you have any question/comment/suggestion/request.

Install Eclipse
First of all download Eclipse from the official website:
Download IDE for Java developers (for example) or the one for C/C++ developer. I’ll download the first for this guide.

Unzip the package in your preferred folder.

Now you can start Eclipse. It will ask you for the workspace folder. You can leave the default.

NOTE: If it doesn’t start after setting launcher options then MOVE eclipse in another folder, maybe without any space within the path name.

Install Java SDK
Download the Java SDK (JDK):
I got the “jdk-6u24-windows-i586.exe” installer.

Install it. Easy :).

Install OpenGL Libraries for Java (Mac and Windows)
I use JOGL for this.
It is maintained by jogamp, there: http://jogamp.org/jogl/www/
Download JOGL from: http://jogamp.org/deployment/webstart/archive/
For Windows I Downloaded: jogl-2.0-b23-20110303-windows-i586.7z (http://jogamp.org/deployment/webstart/archive/jogl-2.0-b23-20110303-windows-i586.7z). For Mac I got the “jogl-2.0-pre-20101104-macosx-universal” package.
Extract it in a reasonable place, maybe in a subfolder named “jogl-2.0″… something like “C:libjogl-2.0” (or equivalent in Mac).

Now we have to bind it with Eclipse IDE. I will explain only the minimum amount of work to do for this (just to have it working) because I know that the first thing that someone wants is have t working and looking into the detail later, when we are already happy that it works! :)
Open Eclipse Preferences (e.g. “Windows->Preferences” in Windows Helios or “Eclipse->Preferences” in Mac Galileo).
In the form open:
Java->Build Path->User Libraries
Click the “New…” button and in the dialog type something sensible in the library name box. Something such as “jogl-2.0”:

Select the newly created entry and click on the “Add JARs…” button. Browse where the jogl package has been extracted and enter in the jar folder. Once in the folder you need to select just those files:

  • gluegen-rt.jar
  • jogl.all.jar
  • nativewindow.all.jar
  • newat.all.jar

Once this is done, you have to set the lib folder for each one of those. Just expand each entry, select the Native library location field, click “Edit…” and browse until you reach the lib folder of the same extracted jogl package:

Repeat this for each entry.

In order to use it in your (any) Java project, go into the project’s properties page (right click on the project in the Project Explorer window and select “Properties”).
In the properties page open the “Java Build Path” page, then select the “Libraries” tab and click the “Add Library…” button. In the selection window select “User Library“:

Click “Next” and select our jogl-2.0 library”

And click “Finish

If everything is done correctly, this should be the final result:

Once this is done you are ready to write and run OpenGL Java apps.

Install CDT Plugin for Eclipse (for C/C++ projects)
Go in:
Help->Install New Software…

From the list select the CDT repository for helios, or add it if it is not there: http://download.eclipse.org/tools/cdt/releases/helios
and select the CDT Main Features:

Then just follow the installation flow (Next, Next, Accept License Agreement, etc)

Restart Eclipse when asked

You can show the icon for quick access clicking on the Open Perspective button or accessing it from the menu Window->Open Perspective. Select “Other…” and select the C/C++ perspective:

Now both Java anc C/C++ perspectives will be available with one click:

Install the C/C++ compiler (Windows)
I use MiniGW to build the project. You can take it from there: http://www.mingw.org/
Install it in an easy-to-reach folder. “C:MiniGW” is perfect for this.
I Install only the C++ Compiler, MSYS Basic System and the MiniGW Developer Toolkit:

Once finished I like to add “C:\MinGW\bin” and “C:\MinGW\msys\1.0\bin” to the path:

This should be enough to meke Eclipse Happy to build our C/C++ projects.

When building C/C++ projects in Windows I use the MiniGW/CDT Toolchain, while In Mac I use MacOSX/GNU.
To set this in Windows, Open Project Preferences and then “C/C++ Build->Tool Chain Editor“. Here select MiniGW GCC, CDT Internal Builder.

In Mac, Open Project Preferencesand then “C/C++ Build->Tool Chain Editor“. Here select MacOSX GCC, GNU Make Builder.

Set OpenGL for C++ (Windows)
Before starting, just remember to edit (almost) always the settings for both configuration (Debug and Release) at the same time. This can save some time and problems when switching configuration.
To build OpenGL applications in C/C++ you generally need (at least) gl.h and glu.h headers, glu32 and opengl32 libraries (lib and dll).
Those headers can come with Windows SDK installation and for example you can find them there: “C:Program FilesMicrosoft SDKsWindowsv7.0AIncludeGL” and “C:Program FilesMicrosoft SDKsWindowsv7.0ALib”, while the runtime dlls should already be in the WindowsSystem32 (or equivalent) folder.
You can find the headers in the “C:MinGWincludeGL” folder as well and I have those one set in my environment.
To set the necessary paths open the Project properties, and select “C/C++ General->Paths and Symbols“.
In the “Include tab you can set useful path for headers and in the “Libraries” tab you can do the same for lib files.
To set Includes, select “GNU C/C++” language (if you are building a C++ project) and add all the paths needed.
In my case I have those set:

Remember to set those values for both Debug and Release configurations.

You can do the same for the Libs:

…and Lib Paths (that you generally need if you are using custom libs):

Note that if you installed MiniGW then the libraries files needed to build the solution are already picked up from there. In the specific they are “libglu32.a” and “libopengl32.a” in “C:MinGWlib” in my case. So you have just to tell Eclipse that you want to use them, as we already did just above, and don’t worry about the libraries paths.

When you run it, you will instead need the DLLs, but as already mentioned they will be already in the WindowsSystem32 folder.

Another thing to note is that some settings are available and configurable through different menus… this can be confusing at first, but doing just few experiments can help in acquiring more confidence when touching those settings. One example of this is the setting we just done, the one related to setting headers and libraries folders. In fact, instead of setting them in the way I described, you can set them always using the Project Properties page, but using the “C/C++ Build->Settings” page. The picture shows the Linker Libraries settings in this section (the same set previously through the other menu):

Installing GLUT for C++ (Windows)
If you are serious about doing something with OpenGL, you will probably need to download another piece of software that simplifies the development of a multi-platform window system independent app.
You can find them there: http://www.opengl.org/resources/libraries/glut/
The latest version (quite old as well) is the 3.7, but I worked with the 3.6 and it does the job.
Just grab a version and unzip it a good place. You can use the previously created “C:lib” folder if you prefer.
To build and run your app you need just few files:

  • glut.h
  • glut.lib
  • glut32.lib
  • glut.dll
  • glut32.dll

I suggest to organise them distributing them in good named folders:

Once this is done, just update the headers and libraries paths to use it.
NOTE: Remember to put the DLL into your app executable folder or in a system folder (WindowsSystem32).

Set OpenGL and GLUT for C++ (Mac)
Setting OpenGL on the Mac is somehow easier from one point of view, but I think that it is trickier to discover how to set-up initially.
It is easier because you should already have all you need (headers and libraries) in your OS.
You can find them in “/System/Library/Frameworks/OpenGL.frameworks/“. So, nothing really to do here.

The nice thing here is that GLUT as well comes with the system, and you should find it in “/System/Library/Frameworks/GLUT.frameworks/” :):

In order to set-up them in Eclipse, open the project properties and go in:
C/C++ Build->Settings->MacOSX C++ Linker->Miscellaneous” and write this in the Linker flags text box:
-framework GLUT -framework openGL -framework Cocoa“. This is the tricky step to find initially I recon.

… and you are ready to go!

Bonus – Install Subversive Plugin for Eclipse
Install SVN if you need one.
As already done for CDT use:
Help->Install New Software…
To install Subversive select the Helios entry and Install the option that you prefer. I selected just the Team Provider and Revision Graph options:

Restart Eclipse

When you will use it the first time it will present the Subversive Connector Discovery form. I select the SVN Kit 1.3.5 options:

I hope that this helped you sorting out some of part of all the settings and the nitty-gritty details that must be addressed in order to successfully build OpenGL applications in Windows or Mac, using Java or C++. In my experience those are often that sort of things that sometime can become frustrating problems when you need to spend hours or days trying to figure out what is missing.

If this helped you or you find this interesting or if you have some tips to share, please leave a comment and link to this page so that other people can benefit from it.