27.7.3.2 Building C API Client Programs Using pkg-config

MySQL distributions contain a mysqlclient.pc file that provides information about MySQL configuration for use by the pkg-config command. This enables pkg-config to be used as an alternative to mysql_config for obtaining information such as compiler flags or link libraries required to compile MySQL applications. For example, the following pairs of commands are equivalent:

mysql_config --cflags
pkg-config --cflags mysqlclient

mysql_config --libs
pkg-config --libs mysqlclient

The last pkg-config command produces flags for dynamic linking. To produce flags for static linking, use this command:

pkg-config --static --libs mysqlclient

On some platforms, the output with and without --static might be the same.

Note

If pkg-config does not find MySQL information, it might be necessary to set the PKG_CONFIG_PATH environment variable to the directory in which the mysqlclient.pc file is located, which by default is usually the pkgconfig directory under the MySQL library directory. For example (adjust the location appropriately):

# For sh, bash, ...
export PKG_CONFIG_PATH=/usr/local/mysql/lib/pkgconfig
# For csh, tcsh, ...
setenv PKG_CONFIG_PATH /usr/local/mysql/lib/pkgconfig

The mysqlconfig.pc installation location can be controlled using the INSTALL_PKGCONFIGDIR CMake option. See Section 2.9.7, “MySQL Source-Configuration Options”.

The --variable option takes a configuration variable name and displays the variable value:

# installation prefix directory
pkg-config --variable=prefix mysqlclient
# header file directory
pkg-config --variable=includedir mysqlclient
# library directory
pkg-config --variable=libdir mysqlclient

To see which variable values pkg-config can display using the --variable option, use this command:

pkg-config --print-variables mysqlclient

You can use pkg-config within a command line using backticks to include the output that it produces for particular options. For example, to compile and link a MySQL client program, use pkg-config as follows:

gcc -c `pkg-config --cflags mysqlclient` progname.c
gcc -o progname progname.o `pkg-config --libs mysqlclient`