Team LiB
Previous Section Next Section

Compiling and Linking

At the source level, the interface to the client library is defined in a set of header files. Generally, MySQL programs include at least the following three files:

#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>

To tell the compiler where to find these files, you might need to specify an -Ipath_name option, where path_name 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 this:

% 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 the contents of 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_name option, where path_name is the pathname to 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 my_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. Invoke it as follows to find out which flags are appropriate for your system:

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

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

    Team LiB
    Previous Section Next Section