mysql linux c找不到头文件解决办法
gcc -c test.c -I /usr/local/mysql/includegcc test.o -o test -L /usr/local/mysql/lib -lmysqlclient./test#include#includevoid main(int argc,char *argv[]){ MYSQL* m_mysql = mysql_ini
gcc -c test.c -I /usr/local/mysql/include
gcc test.o -o test -L /usr/local/mysql/lib -lmysqlclient
./test
#include <mysql.h>
#include <stdio.h>
void main(int argc,char *argv[])
{
MYSQL* m_mysql = mysql_init(NULL);
if (!m_mysql) {
printf("mysql_init failed");
return ;
}else{
printf("mysql_init success");
}
mysql_options(m_mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4");
if (!mysql_real_connect(m_mysql, "localhost", "root", "123456",
"dbname", 3306, NULL, 0)) {
printf("mysql_real_connect failed: %s", mysql_error(m_mysql));
return ;
}else{
printf("mysql connect success");
}
}
参考以下文章
今天编译了一个C连接mysql数据库的程序,出错是找不到mysql.h头文件,现在把它出错解决办法记录下来。
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <mysql.h>
- int main() {
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- char *server = "localhost";
- char *user = "root";
- char *password = "xxxxxx"; /* password */
- char *database = "li";
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn,server,user, password, database, 0, NULL, 0))
- {
- fprintf(stderr, "%sn", mysql_error(conn));
- exit(1);
- }
- if (mysql_query(conn, "show tables"))
- {
- fprintf(stderr, "%sn", mysql_error(conn));
- exit(1);
- }
- res = mysql_use_result(conn);
- while ((row = mysql_fetch_row(res)) != NULL)
- printf("%s\n", row[0]);
- mysql_free_result(res);
- mysql_close(conn);
- }
执行程序过程如下:
连接 :gcc mysql.o -o mysql -L /usr/lib/ -lmysqlclient 这里的-L是指定库文件路径
执行命令默认搜索路径包括/bin,/usr/bin,/usr/local/bin;
更多推荐
所有评论(0)