This post has already been read 2912 times!

MySQL++ is a C++ wrapper for MySQL’s C API.
It is built around the same principles as the Standard C++ Library, to make dealing with the database as easy as dealing with STL containers. In addition, MySQL++ provides facilities that let you avoid the most repetitive sorts of SQL within your own code, providing native C++ interfaces for these common tasks.

MySQL++ (C++ wrapper code source)

The first thing you have to do is include mysql++.h in each module that uses MySQL++.

There are lots of ways to build programs on Unixy platforms. We’ll cover just the most generic way here, Makefiles. We’ll use a very simple example so it’s clear how to translate this to more sophisticated build systems such as GNU Autotools or Bakefile.

“Hello, world!” for MySQL++ might look something like this:


int main()
    mysqlpp::String greeting("Hello, world!");
    std::cout << greeting << std::endl;
    return 0;

Here’s a Makefile for building that program:

CXXFLAGS := -I/usr/include/mysql -I/usr/local/include/mysql++
LDFLAGS := -L/usr/local/lib
LDLIBS := -lmysqlpp -lmysqlclient


    rm -f $(EXECUTABLE) *.o

The *FLAGS lines are where all of the assumptions about file and path names are laid out. Probably at least one of these assumptions isn’t true for your system, and so will require changing.

The trickiest line is the LDLIBS one. MySQL++ programs need to get built against both the MySQL and MySQL++ libraries, because MySQL++ is built on top of the MySQL C API library If you’re building a threaded program, use -lmysqlclient_r instead of -lmysqlclient here.

On some systems, the order of libraries in the LDLIBS line is important: these linkers collect symbols from right to left, so the rightmost library needs to be the most generic. In this example, MySQL++ depends on MySQL, so the MySQL C API library is rightmost.

You might need to add more libraries to the LDLIBS line. -lnsl, -lz and -lm are common. If you study how MySQL++ itself gets built on your system, you can see what it uses, and emulate that.

You may be wondering why we have used both LDLIBS and LDFLAGS here. Some Makefiles you have seen probably try to collect both types of flags in a single variable. Whether that works or not depends on where on the command line those flags appear. Since we’re depending on the standard make rules here, we know have to separate the -l and -L flags due to the place they’re inserted into the link command. If you were writing your own compilation rules, you could write them in such a way that you didn’t have to do this.

Beyond that, we have a pretty vanilla Makefile, thanks in large part to the fact that the default make rules are fine for such a simple program.

The NEXT STEP is to START Eclipse. Create a new C++ project (executable|empty project) and add your code :


using namespace std;

int main() {

	mysqlpp::Connection conn(false);

	if (conn.connect("database", "localhost", "root", "")) {

		mysqlpp::Query query = conn.query("select adresse_id from adresse");
				if (mysqlpp::StoreQueryResult res = {
					cout << "We have:" << endl;
					mysqlpp::StoreQueryResult::const_iterator it;
					for (it = res.begin(); it != res.end(); ++it) {
						mysqlpp::Row row = *it;
						cout << '\t' << row[0] << endl;
				else {
					cerr << "Failed to get item list: " << query.error() << endl;
					return 1;

				return 0;

	} else {
		cerr << "DB connection failed: " << conn.error() << endl;
		return 1;

	return 0;

Now it's necessary to configure the C/C++ Build -> Settings parameters :

1. in GCC C++ Compiler
All options :
-I/usr/include/mysql++ -I/usr/include/mysql -O0 -g3 -Wall -c -fmessage-length=0


2. in CGG C++ Linker
All options :
-L/usr/lib64/mysql -Xlinker -lmysqlpp -Xlinker -lmysqlclient



3. Compile your program and after this Run it :

As far as I can tell, the simplest way to build a C++ project with Eclipse is to set up a Makefile for it as described above, then add an external run configuration for your local make tool.

Comments are closed.

Post Navigation