Using mysql in c programming

I installed ubuntu on a virtual machine. There I installed mysql server sudo apt-get install mysql-server. It worked because I could getmysql-u root -p password

After that I did: 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);
}

When I compile it with

gcc version.c -o version  `mysql_config --cflags --libs`

it works.

But when I compile this one from below

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

I get some errors.

#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);
}

Errors:

"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' "

Can someone explain to me what I did wrong and how to fix it?

I just want to get some data from tables in program C.

+4
source share
1 answer

I suspect that you were actually running

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

but not

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

; mysql_config, , , gcc, --cflags --libs, gcc . gcc .

, mysql_config, gcc, .

+3

Source: https://habr.com/ru/post/1568554/


All Articles