Alan Yu
Alan Yu

Reputation: 85

C code connect to mysql

I have a c code in Linux Ubuntu system.

I used this command to install the library

$ sudo apt-get install libmysqlclient-dev

Then I used a test code to check the installing.

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

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

  exit(0);
}

When I built the code in Netbean by GCC, it shows the error message:

In file included from mysql.c:1:0: /usr/include/mysql/my_global.h:77:23: fatal error: my_config.h: No such file or directory

I have check the include file. They are exist.

Upvotes: 0

Views: 1390

Answers (4)

SuB
SuB

Reputation: 2547

If you are using an IDE, please do these steps:

  1. Add /usr/include/mysql to include path list (-I switch in GCC command-line).
  2. Add /usr/lib/x86_64-linux-gnu to library search path list (-L switch in GCC command-line)
  3. Link to pthread and mysqlclient libraries (-l switch in GCC command-line)

Also you should include MySqlClient library using:

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

Upvotes: 0

Younes
Younes

Reputation: 7

you can try with this For GNU C compiler

  gcc source_file.c -o output_file -std=c99  `mysql_config --cflags --libs`
  ./output_file

Upvotes: 0

Ken Ho
Ken Ho

Reputation: 11

I find that when I build the code as it follows, it works

$ mysql_config --libs
$ mysql_config --cflags
$ gcc -o output_file $(mysql_config --cflags) source_file.c $(mysql_config --libs)
$ ./output_file

Upvotes: 1

Toby Speight
Toby Speight

Reputation: 30709

It looks like you haven't specified the correct include paths on your compilation command.

MySQL doesn't use pkg-config, but has a very similar mysql_config command:

$ mysql_config --cflags
-I/usr/include/mysql -DBIG_JOINS=1  -fno-strict-aliasing   -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing

Normally, you'd use it in your Makefile something like this:

CFLAGS += $(shell mysql_config --cflags)
CXXFLAGS += $(shell mysql_config --cxxflags)
LDLIBS += $(shell mysql_config --libs)

Upvotes: 2

Related Questions