4

I installed ubuntu on a virtual machine. There, I installed mysql server sudo apt-get install mysql-server .This worked, because I could acces mysql-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 this 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 me what I did wrong,and how to fix it?

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

6
  • It would seem you got the command wrong when you are trying to compile as some commands get called with unexpected parameters, some of them being the output of some other command, this is not related to the other file but more likely to a mistake when editing the command. Commented Dec 21, 2014 at 22:41
  • I can't reproduce this behavior. Can you try to test again? Commented Dec 21, 2014 at 22:54
  • 1
    Works for me. Is that the exact command line you're using to compile it? As @CMoi says, the "errors" you get are actually the output from mysql_config. Commented Dec 21, 2014 at 22:54
  • 1
    I suspect you actually ran gcc createdb.c -o createdb -std=c99 `mysql_config` --cflags --libs rather than gcc createdb.c -o createdb -std=c99 `mysql_config --cflags --libs`. Commented Dec 21, 2014 at 23:06
  • 1
    Please do not post the same question on multiple sites. Using mysql in C prorgramming Commented Dec 21, 2014 at 23:35

1 Answer 1

3

I suspect you actually ran

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

rather than

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

That’s not going to work; mysql_config, if it doesn’t get any arguments, is going to print out a bunch of usage instructions, which will be passed to gcc, and then you’ll follow that with --cflags --libs, which gcc also doesn’t understand. gcc is severely confused and complains.

If you make sure those arguments get to mysql_config rather than gcc, everyone will be happy.

Sign up to request clarification or add additional context in comments.

2 Comments

I always ran "gcc createdb.c -o createdb -std=c99 mysql_config --cflags --libs". Thats not the problem...I still have those errors...
I mean,I used gcc createdb.c -o createdb -std=c99 'mysql_config --cflags --libs' with `instead of '

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.