This section provides guidelines for compiling C programs that use the MySQL C API.
You may need to specify an -I option when you
compile client programs that use MySQL header files, so that the
compiler can find them. For example, if the header files are
installed in /usr/local/mysql/include, use
this option in the compile command:
-I/usr/local/mysql/include
MySQL clients must be linked using the -lmysqlclient
-lz options in the link command. You may also need to
specify a -L option to tell the linker where to
find the library. For example, if the library is installed in
/usr/local/mysql/lib, use these options in
the link command:
-L/usr/local/mysql/lib -lmysqlclient -lz
The path names may differ on your system. Adjust the
-I and -L options as
necessary.
To make it simpler to compile MySQL programs on Unix, use the mysql_config script. See Section 4.7.2, “mysql_config — Display Options for Compiling Clients”.
mysql_config displays the options needed for compiling or linking:
shell>mysql_config --cflagsshell>mysql_config --libs
You can run those commands to get the proper options and add them manually to compilation or link commands. Alternatively, include the output from mysql_config directly within command lines using backticks:
shell>gcc -c `mysql_config --cflags` progname.cshell>gcc -o progname progname.o `mysql_config --libs`
To specify header and library file locations, use the facilities provided by your development environment.
On Windows, in your source files, you should include
my_global.h before
mysql.h:
#include <my_global.h> #include <mysql.h>
my_global.h includes any other files needed
for Windows compatibility (such as
windows.h) if you compile your program on
Windows.
You can either link your code with the dynamic
libmysql.lib library, which is just a
wrapper to load in libmysql.dll on demand,
or link with the static mysqlclient.lib
library.
The MySQL client libraries are compiled as threaded libraries, so you should also compile your code to be multi-threaded.
Linking with the single-threaded library
(libmysqlclient) may lead to linker errors
related to pthread symbols. When using the
single-threaded library, please compile your client with
MYSQL_CLIENT_NO_THREADS defined. This can be
done on the command line by using the -D option
to the compiler, or in your source code before including the
MySQL header files. This define should not be used when building
for use with the thread-safe client library
(libmysqlclient_r).
If the linker cannot find the MySQL client library, you might
get undefined-reference errors for symbols that start with
mysql_, such as those shown here:
/tmp/ccFKsdPa.o: In function `main': /tmp/ccFKsdPa.o(.text+0xb): undefined reference to `mysql_init' /tmp/ccFKsdPa.o(.text+0x31): undefined reference to `mysql_real_connect' /tmp/ccFKsdPa.o(.text+0x69): undefined reference to `mysql_error' /tmp/ccFKsdPa.o(.text+0x9a): undefined reference to `mysql_close'
You should be able to solve this problem by adding
-L at the end of your link command, where
dir_path
-lmysqlclientdir_path represents the path name of
the directory where the client library is located. To determine
the correct directory, try this command:
shell> mysql_config --libs
The output from mysql_config might indicate other libraries that should be specified on the link command as well. You can include mysql_config output directly in your compile or link command using backticks. For example:
shell> gcc -o progname progname.o `mysql_config --libs`
If an error occurs at link time that the
floor symbol is undefined, link to the math
library by adding -lm to the end of the
compile/link line.
If you get undefined reference errors for the
uncompress or compress
function, add -lz to the end of your link
command and try again.
Similarly, if you get undefined-reference errors for other
functions that should exist on your system, such as
connect(), check the manual page for the
function in question to determine which libraries you should add
to the link command.
If you get undefined-reference errors such as the following for functions that don't exist on your system, it usually means that your MySQL client library was compiled on a system that is not 100% compatible with yours:
mf_format.o(.text+0x201): undefined reference to `__lxstat'
In this case, you should download the latest MySQL or MySQL Connector/C source distribution and compile the MySQL client library yourself. See Section 2.9, “Installing MySQL from Source”, and Section 17.4, “MySQL Connector/C”.

User Comments
Add your own comment.