[ Note: this post was updated on Dec 2, 2009 to correct dashes. In the original post, dash dash -- was showing up as a single dash. This is noted in the comments and has been corrected in the text below.]
These are very (very) basic instructions for setting up a very simple application with Microsoft Visual C++ 2008 Express to use the MySQL Embedded Library (libmysqld).
The MySQL Embedded Library is a full version of the server that is available as a dynamic or static library. Developers can run the library inside their application. It’s extremely fast, easy to distribute and it’s ideal for stand-alone applications. More information about the library can be found at http://dev.mysql.com/doc/refman/5.1/en/libmysqld.html
Having little experience using Visual C++, I’ve struggled to find instructions for setting up a project to use a dynamic library. I finally got it to work thanks to Ulf Wendel’s blog about building a windows client using the Connector/C++ ( http://blog.ulf-wendel.de/?p=215 ). I was able to take his tutorial, substitute the appropriate libmysqld entries and compile the project. If you get lost following my instructions, refer back to Ulf’s blog.
To develop a libmysqld application, you’ll need data files, the error message file ( errmsg.sys ), the appropriate libraries and header files. To get these files, I’d recommend downloading and installing the full version of the server. Also, it’s easier to design and build your database using the standard version of the server. Once you have some data, stop the MySQL service and then create your application using libmysqld.
Here’s the process that I used.
Download the Windows MSI Installer (x86) from http://dev.mysql.com/downloads. For this example, I downloaded and installed version 5.1.34. I selected the option for a standard install. After the install, you can create tables and design your database as needed. For this example, I’m going to use the “user” table, which installs with MySQL.
After the install, go to the services control panel ( Start | Control Panel | Administrative Tools | Services ) and stop MySQL. You might also change the Startup type to “manual” to ensure that it doesn’t start on reboot and cause a conflict with your project.
Locate required directories for setting up the project:
1) Data Files
On Windows, the default location for data files is C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data We’ll be accessing the user.MYD MyISAM table, which is located in the .\mysql subdirectory. Similarly, If you were to create a database named world with a city table, there would be a corresponding world directory and city.MYD MyISAM table in the data directory.
2) Error Message File
The error message file ( errmsg.sys ) is required to start libmysqld. By default, it’s located at C:\Program Files\MySQL\MySQL Server 5.1\share\english
3) Header Files
C:\Program Files\MySQL\MySQL Server 5.1\include
4) Embedded Library
C:\Program Files\MySQL\MySQL Server 5.1\Embedded\DLL\release
Copy the libmysqld.dll from the embedded library path (above) to the c:\windows\system32 directory (or anywhere in your search path). For deployment, you’ll probably want to include the embedded library with the executable.
Time to setup up Microsoft Visual C++ Express
Launch Microsoft Visual C++ Express. From the main menu, select File | New | Project
Choose a Win32 Console Application

Select OK and Finish.
Right-click the project name in the Solution Explorer and select Properties.

Expand Configuration Properties, C/C++ Properties and add the following for the Additional Include Directories:
C:\Program Files\MySQL\MySQL Server 5.1\include

Expand Linker, select General and add the following to Additional Library Directories: C:\Program Files\MySQL\ MySQL Server 5.1\ Embedded\ DLL\release
Select Linker, Input and add the following for Additional Dependencies: libmysqld.lib
Select OK to save the setting and return to the project.
Copy the following into the source file ( my copy is named sample.cpp ) Overwrite any existing code in the file.
#include “stdafx.h”
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
#include <mysql.h>
MYSQL *mysql;
MYSQL_RES *results;
MYSQL_ROW record;
static char *server_options[] = { “mysql_test”, “--datadir=C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.1/data“,
“--language=C:/Program Files/MySQL/MySQL Server 5.1/share/english", NULL };
int num_elements = sizeof(server_options)/ sizeof(char *) – 1;
static char *server_groups[] = { “libmysqld_server”, NULL };
int main(int argc, char* argv[])
{
int retval;
retval = mysql_library_init(num_elements, server_options, (char **) server_groups);
mysql = mysql_init(NULL);
mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
mysql_real_connect(mysql, NULL,“root”,NULL, “mysql”, 0, NULL, 0);
mysql_query(mysql, “SELECT user, host FROM user”);
results = mysql_store_result(mysql);
while((record = mysql_fetch_row(results))) {
printf(“%s – %s \n”, record[0], record[1]);
}
mysql_free_result(results);
mysql_close(mysql);
return 0;
}
At this point, you should be able to build and run your project. The output should look like this.

It’s a simple listing of the users in the mysql table.
The following line of code is critical:
static char *server_options[] = { “mysql_test”, “--datadir=C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.1/data“,
“--language=C:/Program Files/MySQL/MySQL Server 5.1/share/english", NULL };
You must have a datadir and a language file. Note the forward slash in the directory paths. Of course, these paths can be located anywhere. You’ll probably want to move these folders to a subdirectory under your application install. If you don’t see an error log in the data directory, that’s also an indicator that these paths are incorrect.
I hope someone finds this useful.