fatal error mysql.h:No such file or directory during compilation

前端 未结 4 839
滥情空心
滥情空心 2021-01-18 14:47

I have the following code

#include 
#include 

int main(int argc, char **argv)
{
  printf(\"MySQL client version: %s\\n\",          


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 14:56

    I do not know if there is some variation in your operation system. Mine is Arch Linux, and I have installed mariaDB. Within the package, there is a program called 'mysql_config' which can provide the right way of compile your program. By runnig

    $ mysql_config --help
    Usage: /usr/bin/mysql_config [OPTIONS]
    Options:
        --cflags         [-I/usr/include/mysql]
        --include        [-I/usr/include/mysql]
        --libs           [-L/usr/lib -lmysqlclient -lpthread -lz -lm -lssl   -lcrypto -ldl]
        --libs_r         [-L/usr/lib -lmysqlclient_r -lpthread -lz -lm -lssl -lcrypto -ldl]
        --plugindir      [/usr/lib/mysql/plugin]
        --socket         [/run/mysqld/mysqld.sock]
        --port           [0]
        --version        [10.0.17]
        --libmysqld-libs [-L/usr/lib -lmysqld]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/include/mysql]
                pkglibdir     [/usr/lib]
                plugindir     [/usr/lib/mysql/plugin]
    

    you can see the control flags of the program. With your program, i used the following:

    $gcc main.c -o main $(mysql_config --libs --cflags)
    

    and then, by runnig the the new program 'main'

    $./main
    MySQL client version: 10.0.17-MariaDB
    

    which clearly worked out!

    So, i'm sure that there a few others ways of doing this, but now this is fine for me.

    Tip

    Run the command

    $mysql_config --libs --cflags
    

    to see the exacly flags that mysql_config produces. Enjoy!

提交回复
热议问题