Building a MySQL client program on Linux

Building a C program (client) to connect to MySQL server is simple, but at times it can become a little problematic for a newbie to find all the necessary dependencies (includes/libs). The best option that I would recommend is to use the mysql_config script that comes bundled with the MySQL distribution. This shell script when executed with some options, would print all necessary compiler options/flags/switches required to build a MySQL client program. Lets see it in action!

  @file client_version.c
  Display the client version.

#include                               /* All public MySQL APIs */

int main()
    printf("MySQL client version: %s\n", mysql_get_client_info());
    return 0;

# mysql_config's output
> .../bin/mysql_config --cflags --libs
-I/  -fPIC -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing
-L/ -lmysqlclient -lpthread -lm -lrt -ldl

# and now pass it down to the compiler..
> gcc client_version.c `/home/nirbhay/project/repo/bld/trunk/bld/bin/mysql_config --cflags --libs`
.. done

> ./a.out 
MySQL client version: 5.7.2-m12

# whoa.. that worked!

Sometimes, the generated binary would complain of missing shared object file. It can be solved by setting up LD_LIBRARY_PATH to the lib directory containing file.

> ./a.out 
./a.out: error while loading shared libraries: cannot open shared object file: No such file or directory
nirbhay@Nirbhay-lenovo:~/project/sandbox/mysql$ ldd a.out =>  (0x00007fffce1ee000) => not found => /lib/x86_64-linux-gnu/ (0x00007fca652e6000)
	/lib64/ (0x00007fca656a3000)

MySQL client version: 5.7.2-m12

Leave a Reply

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