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.

Comments

  1. Jan Paulussen

    2 years ago

    When I try to install this library with:

    sudo apt-get install libmysqlclient-dev

    it gives me this error indicating it does not seem to exist anymore:

    Reading package lists… Done
    Building dependency tree… Done
    Reading state information… Done
    Package libmysqlclient-dev is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
    libmariadb-dev-compat libmariadb-dev

    E: Package ‘libmysqlclient-dev’ has no installation candidate

    1. Jan Paulussen

      2 years ago

      Solved I guess with using this instead:
      sudo apt-get install default-libmysqlclient-dev

  2. tom lee

    4 years ago

    thank you very much!!

  3. Dmitry Gornostaev

    4 years ago

    Thank you very much! Now I can compile C programs with mariadb-c-connector.

  4. Your best Fan

    9 years ago

    Hi,
    Nice tutorial !!!
    I have a question about setup. :/
    I tried to follow installation about Netbeans but i run on Linux 64bits.
    The compiler doesn’t find the library even if i write the right way : #include
    I tried to give the full way #include
    The files and the function and structure were found.
    But they are an error with the variable :
    MYSQL *mysql1;

    I don’t know what is the problem and call for your help :)

    Regards,

    Your best fan !!!!

Comments

Your email address will not be published. Required fields are marked *