简体   繁体   中英

C code using gcc cannot link to mysql header?

I'd like to build on this post, because my symptoms are identical, but the solution seems to be something else.

I am working in a Ubuntu container (Ubuntu 16.04.3 LTS), trying to compile a toy C program that will eventually connect to an SQL server. First things first: I have the latest libmysqlclient-dev installed:

root@1234:/home# apt install libmysqlclient-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
libmysqlclient-dev is already the newest version (5.7.24-0ubuntu0.16.04.1).
0 upgraded, 0 newly installed, 0 to remove and 88 not upgraded.
root@1234:/home#

And when I look in the /usr/include/mysql directory, I see the critical header file I'll need:

root@1234:/home# ls -l /usr/include/mysql  | grep mysql.h
-rw-r--r-- 1 root root 29207 Oct  4 05:48 /usr/include/mysql/mysql.h
root@1234:/home#

So far, so good. Now I found this little toy program from here :

#include <stdio.h>
#include "/usr/include/mysql/mysql.h"

int main() {
  MYSQL mysql;
  if(mysql_init(&mysql)==NULL) {
    printf("\nInitialization error\n");
    return 0;
  }
  mysql_real_connect(&mysql,"localhost","user","pass","dbname",0,NULL,0);
  printf("Client version: %s",mysql_get_client_info());
  printf("\nServer version: %s",mysql_get_server_info(&mysql));
  mysql_close(&mysql);
  return 1;
}

Now, following the advice of the previous post, I compile using the "-I" option to point to the mysql.h header file. (Yes, I am required to use GCC here):

root@1234:/home# gcc -I/usr/include/mysql sqlToy.c
/tmp/cc8c5JmT.o: In function `main':
sqlToy.c:(.text+0x25): undefined reference to `mysql_init'
sqlToy.c:(.text+0x69): undefined reference to `mysql_real_connect'
sqlToy.c:(.text+0x72): undefined reference to `mysql_get_client_info'
sqlToy.c:(.text+0x93): undefined reference to `mysql_get_server_info'
sqlToy.c:(.text+0xb4): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
root@1234:/home#

Belly flop! The compiler has no idea what all the mySQL functions are, even though I've done all I can think of to point to that header file. As a sanity check, I made sure those mySQL functions are indeed in that header file; here's one as an example:

root@1234:/home# more /usr/include/mysql/mysql.h | grep mysql_init
  libmysqlclient (exactly, mysql_server_init() is called by mysql_init() so
MYSQL *         STDCALL mysql_init(MYSQL *mysql);
root@1234:/home#

So while I've pointed the compiler to the mysql.h header file in both my code AND with the "-I" option, it still has no clue what those 'mysql' functions are. The "-I" option was the solution in the previous post, but is not working for me here.

So... What might be the problem? I'm assuming this isn't a compiling problem, but maybe a linking one? In other words, I'm showing GCC where the mysql.h file is, but he is still not using it when processing the code?

Headers are not libraries. You are including the MySQL header during compilation, so these functions are defined, but you are not linking against the library which actually provides those functions.

These functions are provided by the libmysqlclient library, so you need to add the -lmysqlclient flag to your command line to fix this. (Note that this is a lower-case l , not an I .)

Additionally, since you are adding /usr/include/mysql to your system header path, you can include the library as

#include <mysql.h>

You do not need to -- and should not! -- specify the full path to the library in the #include directive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM