我想在我的 C++ 项目中使用此 C 代码: mysql.c :
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
#include <string.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "rebourne"; /* set me first */
char *database = "mydb";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
它用 gcc 编译:
gcc -o output-file $(mysql_config --cflags) mysql.c $(mysql_config --libs)
但不适用于 g++ ..
你会怎么做?
编辑: 错误是: 退出(1);未在此范围内声明
最佳答案
首先要做的是:如果您向我们展示编译错误,可能会更有帮助。
话虽如此,我最初的直觉是建议:
extern "C"
{
#include <mysql.h>
#include <stdio.h>
#include <string.h>
}
目前这是一个猜测,但如果没有实际的错误消息,这就是您将获得的最佳结果。
哦,也许将文件重命名为以 .cpp、.c++ 或 .C(大写)结尾会有所帮助。
关于c++ - 在 C++ 项目中使用 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2396467/