Compiling and Linking

At the source level, the interface to the client library is defined in the mysql.h header file, which your own source files can include:

#include <mysql.h> 

To tell the compiler where to find this file, you may need to specify an -Ipath option, where path is the pathname to the directory where the MySQL header files are installed. For example, if your MySQL header files are installed in /usr/include/mysql or /usr/local/mysql/include, you can compile a source file my_func.c by using commands that look something like the following:

% gcc -I/usr/include/mysql -c my_func.c 
% gcc -I/usr/local/mysql/include -c my_func.c

If you need to access other MySQL header files, they can be found in the same directory as mysql.h. For example, mysql_com.h contains constants and macros for interpreting query result metadata. The header files errmsg.h and mysqld_error.h contain constants for error codes. (Note that although you might want to look at mysql_com.h to see what's in it, you don't actually need to include this file explicitly because mysql.h does so. Including mysql.h thus gives your program access to mysql_com.h as well.)

At the object level, the client library is provided as the mysqlclient library. To link this library into your program, specify -lmysqlclient on the link command. You'll probably also need to tell the linker where to find the library using a -Lpath option, where path is the directory where the library is installed. For example:

% gcc -o myprog my_main.o my_func.o -L/usr/lib/mysql -lmysqlclient 
% gcc -o myprog _main.o my_func.o -L/usr/local/mysql/lib -lmysqlclient

If a link command fails with "unresolved symbol" errors, you'll need to specify additional libraries for the linker to search. Common examples include the math library (-lm) and the zlib library (-lz or -lgz).

An easy way to determine the proper header file directories for compiling or library flags for linking is to use the mysql_config utility, available as of MySQL 3.23.21. Invoke it as follows to find out which flags are appropriate for your system:

% mysql_config --cflags 
% mysql_config --libs
-L'/usr/local/mysql/lib/mysql' -lmysqlclient -lz -lcrypt -lnsl -lm

The output shown is illustrative, and likely will be different on your system.