-1

I'm trying to get started with using mySQL C API for a project that I'm hoping to complete.

I've downloaded the mySQL Community Server version and the mySQL Connector/C from the official site.

Q1: Do I also need to download Connector/ODBC? What is the difference?

So, this is a basic program that I learnt and am trying to compile and link:

#include<stdio.h>
#include<mysql.h>

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

    exit(0);
}

I'm extremely confused as to what commands for compilation and linking I should use. When I do the following, this happens:

gcc mySQL.c -I/usr/local/mysql/include
Undefined symbols for architecture x86_64:
  "_mysql_get_client_info", referenced from:
      _main in mySQL-a3f748.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can someone just help me out with this? I've struggled a lot and it all seems extremely confusing.

My question is about compiling and linking mySQL C API libraries and not the error.

ritika_13
  • 51
  • 1
  • 8

1 Answers1

1

The header file <mysql.h> only declares the functions and types needed. The actual function definition (its implementation) is in a library you need to link with.

You do that with the -l (lower-case L) option:

gcc mySQL.c -I/usr/local/mysql/include -lmysql

However, since you seem to have installed MySQL in a non-standard location, you might have to use the -L option to specify where the library is located (similar to the -I option):

gcc mySQL.c -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysql

This should at least make your program build. But there is still another issue that might come up if your MySQL library is not a static library but a dynamic library (i.e. a "DLL"), because the run-time loader will not know the location of the dynamic library. You need a special linker-flag for that too:

gcc mySQL.c -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysql -Wl,-rpath=usr/local/mysql/lib 
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621