Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Compiling a C or C++ program that includes mysql.h fails

Errorfatal error: mysql.h: No such file or directory

Short answer

The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path. Install the dev package, then let mysql_config supply the compile and link flags. If the build then fails with undefined reference to mysql_init, the linker is missing -lmysqlclient.

  • Applies to MySQL client library 8.0 and 8.4, gcc 13, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the system where mysql.h is. If the search finds nothing, the development package is missing. If it finds a path, the compiler just is not told about it.

    find /usr /opt -name mysql.h 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1fatal error: mysql.h: No such file or directory, and find shows no mysql.hThe MySQL client development package is not installedFix
    2. 2fatal error: mysql.h: No such file or directory, while find shows mysql.h in a folder such as /usr/local/mysql/includeThe header exists, but the compiler is not given its folderFix
    3. 3undefined reference to `mysql_init' (and other mysql_* names)It compiles, but the linker cannot find the MySQL functionsFix

What does the error look like?

  • fatal error: mysql.h: No such file or directory
  • undefined reference to `mysql_init'
  • undefined reference to `floor'

Cause 1 of 3

The MySQL client development package is not installed

1Check

The header ships in a development package. On Ubuntu, the file list of libmysqlclient-dev includes /usr/include/mysql/mysql.h, /usr/bin/mysql_config and a mysqlclient.pc file.

dpkg -L libmysqlclient-dev 2>/dev/null | grep -E 'mysql.h|mysql_config'

2Fix

Install the development package, then confirm the header exists.

sudo apt update
sudo apt install libmysqlclient-dev
ls /usr/include/mysql/mysql.h

3Verify

mysql_config answers and prints a flag for the include folder.

mysql_config --cflags

Confirmed by packages.ubuntu.com

Did this fix it?

Cause 2 of 3

The header exists, but the compiler is not given its folder

1Check

A header outside the default search path needs an -I option. The MySQL manual says to pass the include directory with -I, or to ask mysql_config for it.

mysql_config --cflags

2Fix

Put the output of mysql_config on the compile line. For a copy installed in a custom folder, use -I yourself.

gcc `mysql_config --cflags` -o app app.c `mysql_config --libs`
# custom install location, by hand:
gcc -I/usr/local/mysql/include -o app app.c -L/usr/local/mysql/lib -lmysqlclient

3Verify

The compile step passes the include stage and either finishes or moves on to a linker message.

./app

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

It compiles, but the linker cannot find the MySQL functions

1Check

That message comes from the link step, so the header is already found. The library is missing from the link command, or it appears before the source file that needs it.

mysql_config --libs

2Fix

Add the flags from mysql_config --libs at the end of the command. Do not add -lssl or -lcrypto, because the client library already contains SSL support. A message about floor means the math library is missing: add -lm.

gcc -o app app.c `mysql_config --libs`
# extra flag only if the linker names floor:
gcc -o app app.c `mysql_config --libs` -lm

3Verify

The linker finishes and the program starts.

./app

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Build with mysql_config (or pkg-config --cflags --libs mysqlclient) in the Makefile instead of hard-coded paths.
  • Put the libraries after the source files on the link line.
  • Install the dev package in the Dockerfile or CI image that builds the program, not only on your laptop.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.