【问题标题】:Using mysql in c programming在c编程中使用mysql
【发布时间】:2014-12-21 22:37:35
【问题描述】:

我在虚拟机上安装了 ubuntu。在那里,我安装了mysql服务器sudo apt-get install mysql-server。这很有效,因为我可以访问mysql-u root -p password

在那之后,我做了:sudo apt-get install libmysqlclient-dev

#include <my_global.h>
#include <mysql.h>

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

  exit(0);
}

当我用

编译它时
gcc version.c -o version  `mysql_config --cflags --libs`

它有效。

但是当我从下面编译这个时

gcc createdb.c -o createdb -std=c99  `mysql_config --cflags --libs`

我遇到了一些错误。

#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{  
  MYSQL *con = mysql_init(NULL);

  if (con == NULL) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      exit(1);
  }

  if (mysql_real_connect(con, "localhost", "root", "root_pswd", 
          NULL, 0, NULL, 0) == NULL) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }  

  if (mysql_query(con, "CREATE DATABASE testdb")) 
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      mysql_close(con);
      exit(1);
  }

  mysql_close(con);
  exit(0);
}

错误:

"Usage:: No such file or directory
[OPTIONS]: No such file or directory
Options:: No such file or directory
[-I/usr/include/mysql: No such file or directory
[-L/user/lib/x86_64-linux-gnu: No such file or directory
.
.
.
unrecognized command line option '--cflags'
unrecognized command line option '--libs'
.
.
unrecognized command line option '--socket'
unrecognized command line option '--port' "

谁能解释我做错了什么,以及如何解决?

我只想从 C 程序中的表中获取一些数据。

【问题讨论】:

  • 当您尝试编译时,您似乎弄错了命令,因为某些命令使用意外参数调用,其中一些是其他命令的输出,这与其他文件无关但在编辑命令时更可能出错。
  • 我无法重现此行为。可以再试一次吗?
  • 为我工作。那是您用来编译它的确切命令行吗?正如@CMoi 所说,您得到的“错误”实际上是mysql_config 的输出。
  • 我怀疑您实际上运行的是gcc createdb.c -o createdb -std=c99 `mysql_config` --cflags --libs 而不是gcc createdb.c -o createdb -std=c99 `mysql_config --cflags --libs`
  • 请不要在多个网站上发布相同的问题。 Using mysql in C prorgramming

标签: mysql c ubuntu gcc


【解决方案1】:

我怀疑你真的跑了

gcc createdb.c -o createdb -std=c99 `mysql_config` --cflags --libs

而不是

gcc createdb.c -o createdb -std=c99 `mysql_config --cflags --libs`

那是行不通的; mysql_config,如果它没有得到任何参数,将打印出一堆使用说明,将传递给gcc,然后你会跟着--cflags --libsgcc 也是不明白。 gcc 严重困惑并抱怨。

如果您确保这些参数到达 mysql_config 而不是 gcc,那么每个人都会很高兴。

【讨论】:

  • 我总是运行“gcc createdb.c -o createdb -std=c99 mysql_config --cflags --libs”。那不是问题...我仍然有这些错误...
  • 我的意思是,我使用 gcc createdb.c -o createdb -std=c99 'mysql_config --cflags --libs' 和 `instead of '
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-12
  • 2016-02-25
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多