I'm a C++ beginner. What's an easy to use (in terms of both installation and use in code) library for using MySQL in C++? Is there one that's a lot more commonly used than others? Note that I'm using MySQL 4.1.22 on GNU/Linux.

I've found the following:

I think SQLAPI++ abstracts away which database you're using, which is good but not crucial; and I'm not sure how complicated it is to install.

Is one of these libraries particularly easy to install and use? Is one considered something of a "standard" when it comes to C++ and MySQL?

If someone has any other libraries to recommend, then post them :)

These All libraries are good, but for linux, i would advise to use MySQL wrapped, as it has own code for linux.

Thank you.

Do others have any suggestions or tips?

I'm trying out MySQLWrapped, but am having a strange problem that I can't figure out. After downloading mysqlwrapped-1.6.tar.gz, running 'make' and mimicing the MySQLWrapped example, I get the following "undefined reference" error:

[~/public_html/cpp/mysqlwrapped-1.6]# g++ test2.cpp
/tmp/ccFHonFH.o(.text+0xeb): In function `main':
: undefined reference to `Database::Database(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, IError*)'
/tmp/ccFHonFH.o(.text+0x2ac): In function `main':
: undefined reference to `Database::~Database()'
collect2: ld returned 1 exit status

Here's the test2.cpp file that gives the error (login info omitted):

#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string>

#include "Database.h"
#include "Query.h"

int main()
{
    Database db("localhost", "user", "password", "database");
}

I also tried doing #include "libmysqlwrapped.h" instead of Database.h and Query.h, but that gives the same error.

Most likely there's something simple I'm doing wrong or omitting, though I have no idea what :-/

I'm trying out MySQLWrapped, but am having a strange problem that I can't figure out. After downloading mysqlwrapped-1.6.tar.gz, running 'make' and mimicing the MySQLWrapped example, I get the following "undefined reference" error:

[~/public_html/cpp/mysqlwrapped-1.6]# g++ test2.cpp
/tmp/ccFHonFH.o(.text+0xeb): In function `main':
: undefined reference to `Database::Database(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, IError*)'
/tmp/ccFHonFH.o(.text+0x2ac): In function `main':
: undefined reference to `Database::~Database()'
collect2: ld returned 1 exit status

Here's the test2.cpp file that gives the error (login info omitted):

#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string>

#include "Database.h"
#include "Query.h"

int main()
{
    Database db("localhost", "user", "password", "database");
}

I also tried doing #include "libmysqlwrapped.h" instead of Database.h and Query.h, but that gives the same error.

Most likely there's something simple I'm doing wrong or omitting, though I have no idea what :-/

I've used MySQL++ with success with Visual Studio 2008. Haven't had any experience with anything else, but I got MySQL++ to almost work "out of the box". I couldn't make their samples work, but I got my own code to work using their skeleton and it worked well with not much tweaking.

>I get the following "undefined reference" error

You need to link with the libraries too. Simply including header files isn't enough; at the linking stage the linker needs the actual implementations for the functions which were used in the program. Look in the documentation for which libraries you need to link with, the search paths you should add, etc.

>I get the following "undefined reference" error

You need to link with the libraries too. Simply including header files isn't enough; at the linking stage the linker needs the actual implementations for the functions which were used in the program. Look in the documentation for which libraries you need to link with, the search paths you should add, etc.

Thanks. Though I don't think there's any real documentation to MySQLWrapped (the readme links to a defunct website), and while the developer's site has examples and tutorials I don't see anything about compiling.

I checked the Makefile though and it had the following lines:

INCLUDE =       -I/usr/devel/include
LIBS =          -L/usr/local/lib/mysql -lmysqlclient -lz

So I tried adding "-I/usr/devel/include" and "-L/usr/local/lib/mysql -lmysqlclient -lz" while compiling, both individually and together, however I still got the same "undefined reference" error.

I think the link step is missing the wrapper library itself (libmysqlwrapped) in which the Database class' code is. So you need to find out where that library is and link with it too.

Turns out that the Makefile had some incorrect settings, in terms of where mysql is located on my server:

INSTALL_PREFIX = /usr/devel
INCLUDE =       -I/usr/devel/include
LIBS =          -L/usr/local/lib/mysql -lmysqlclient -lz

These should actually be /usr, -I/usr/include, and -L/usr/lib/mysql -lmysqlclient -lz. However even after editing the Makefile and re-making and doing ""g++ test2.cpp -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz", I still got the same "undefined reference" error.

I think the link step is missing the wrapper library itself (libmysqlwrapped) in which the Database class' code is. So you need to find out where that library is and link with it too.

Hmm, you seem to be right - here's what "make" outputs:

g++  -Wall -g -O2 -I/usr/include  -MD  -c -o Database.o Database.cpp
g++  -Wall -g -O2 -I/usr/include  -MD  -c -o Query.o Query.cpp
g++  -Wall -g -O2 -I/usr/include  -MD  -c -o StderrLog.o StderrLog.cpp
g++  -Wall -g -O2 -I/usr/include  -MD  -c -o SysLog.o SysLog.cpp
ar cru libmysqlwrapped.a Database.o Query.o StderrLog.o SysLog.o enum_t.o set_t.o 
ranlib libmysqlwrapped.a
cat IError.h StderrLog.h SysLog.h \
        enum_t.h set_t.h Database.h Query.h \
        > libmysqlwrapped.h

The "-c" flag means omit linking. But I didn't see this in the Makefile anywhere, so I don't know where it comes from.

Anyway, I've decided to go with MySQL++ instead, which seems to be the standard so it's probably the best choice as well. Took me a while to install MySQL++, I had some problems, but eventually I got it working.

When compiling with MySQL++, I need to add the flags "-I/usr/include/mysql -I/usr/local/include/mysql++ -lmysqlpp", e.g.:

g++ test.cpp -I/usr/include/mysql -I/usr/local/include/mysql++ -lmysqlpp

I might also need to add "-L/usr/lib/mysql –L/usr/local/lib/mysql++" in some instances, I'm not sure when they're necessary. but the include and -lmysqlpp flags are necessary, otherwise I get all sorts of "undefined reference" errors.

Is there anything I can do so that I don't need to input these flags every time?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.