2

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?

Pete
  • 1,511
  • 2
  • 26
  • 49
  • I see no `-l` option in your gcc line, `-I` is used to specify additional directories to search for header files. Furhtermore `-l` is used to link in a library, not header files. The actual mysql binary code is somewhere in a library (or multiple libraries), it's that library that needs to get linked in. That's different from a header file. You are correct, it is a linking problem, `ld` is the linker. – yano Nov 12 '18 at 18:30

1 Answers1

2

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.

  • I've since changed my #include statement to this: #include – Pete Nov 12 '18 at 19:04
  • and now I compile with this: [[ gcc -lmysqlclient sqlToy.c ]] and get this compilation error: [[ sqlToy.c:2:19: fatal error: mysql.h: No such file or directory ]] Variations on this command give the earlier error... I'm close, just missing something fundamental... – Pete Nov 12 '18 at 19:06
  • Ah HA!!! Thanks to duskwuff and yano's comments, I figured out the syntax: [[ gcc -Wall sqlToy.c -lmysqlclient ]] Nice! Thank you!!! – Pete Nov 12 '18 at 19:22