可先通过以下指令查看mysql库文件所在路径
mysql_config
再通过以下指令编译指定的C文件
gcc -I /usr/include/mysql test.c -L /usr/lib64/mysql -lmysqlclient -o test
#include <stdlib.h>
#include <stdio.h>
#include <mysql.h>
int main()
{
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
printf("%s", "error\n");
return EXIT_FAILURE;
}
conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", "test", 3306, NULL, 0);
if (conn_ptr) {
printf("%s", "connect success\n");
} else {
printf("%s", "connect failed\n");
}
mysql_close(conn_ptr);
return 0;
}