Some of this is based on the excellent guide here.

Installing The MySQL C libraries

MySQL comes with C-libraries:


sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libmysqlclient-dev

Using In A Geany Project

Include the library in your source files


#include <mysql/mysql.h>

Add these to the LIBS and CFLAGS sections of your makefile


`mysql_config --libs`

`mysql_config --cflags`

Being Able To Compile Using NetBeans For Windows

Go to: Project Properties > Build > Linker > Compilation Line > Additional Options > paste in this:
         `mysql_config –cflags` `mysql_config –libs`

Note the single quote marks are correct and should be used as is!

Being Able To Compile Using Eclipse For Windows

If you are not coding on the RPi itself, for instance developing using eclipse for windows, you need to copy the mysql library files if your cross compiler toolchain doesn't have them.

The following is not based on any expert knowledge of ours, it is instead the results of our trial and error hitting it with a hammer until it worked…

1 – Get the header files

Copy this directory on the RPi:


/usr/include/mysql

to your projects directory.  Use this to include it in your C code file:


#include "mysql/mysql.h"
2 – Create Object Files

Create a file called main.c containing some code which uses the mysql library, e.g. this:


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

#include <mysql.h>

int main(int argc, char **argv)
{
	MYSQL *mysql1;
	mysql1 = mysql_init(NULL);
}

Save it in a directory on the RPI, then on the command line go to that directory and use this to compile it:


g++ main.c `mysql_config --cflags` `mysql_config --libs`

Now use this to find out the directory used:


mysql_config --libs

For us it gives this:

-L/usr/lib/arm-linux-gnueabihf -lmysqlclient -lpthread -lz -lm -lrt -ldl

So, from directory:

/usr/lib/arm-linux-gnueabihf

copy the files

libmysqlclient.a
libmysqlclient.so
(there's also files libmysqlclient_r.a and libmysqlclient_r.so in there but we've not bothered with them)

Paste them into your windows machine folder:

C:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\arm-unknown-linux-gnueabi\sysroot\usr\lib\

Now in eclipse, right click your project > Properties > C/C++ Build > Settings > Cygwin C++ Linker > Libraries.  Add a new library entry:

mysqlclient

This works but building the project fails because the library references other files which need to be added in also.  For us files listed in the eclipse build warnings we're all in this folder:

/lib/arm-linux-gnueabihf/

But if you need to search install and use mlocate to find them on the RPi.

Find each one and copy it into the same cygwin windows library folder.  These are the files we needed:

ld-linux.so.3
ld-linux-armhf.so.3
libmysqlclient.a
libmysqlclient.so
libz.so.1

That's it, for us this worked fine under rasbian and we could access our mysql database no problems.  I guess if the mysql library gets updated on the RPi then the files might need to be found and re-copied again so something to bear in mind if problems appear after updating the RPi ever.

 

 

 

 

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.