Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc wont compile and run MySQL C libraries

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

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

~$ gcc -o mysql-test MySQL-Test.c

im trying to execute this test program from terminal but get the following error message:

/tmp/cceEmI0I.o: In function main': MySQL-Test.c:(.text+0xa): undefined reference tomysql_get_client_info'

what is wrong? my system is ubuntu

like image 404
JB87 Avatar asked Aug 03 '10 11:08

JB87


3 Answers

MySQL comes with a special script called mysql_config. It provides you with useful information for compiling your MySQL client and connecting it to MySQL database server.

Pass --libs option - Libraries and options required to link with the MySQL client library.

$ mysql_config --libs

Typical Output:

-L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto

Now you can add this to your compile/link line:

gcc -o mysql-test MySQL-Test.c $(mysql_config --libs)
like image 56
codaddict Avatar answered Oct 04 '22 02:10

codaddict


You need gcc -o mysql-test MySQL-Test.c -L/usr/local/mysql/lib -lmysqlclient -lz

Replace -L/usr/local/mysql/lib with wherever you client library is (if it isn't already in your libpath)

See the MySql instructions for building clients.

like image 37
deinst Avatar answered Oct 04 '22 02:10

deinst


For uses of Netbeans on Linux

Open you make file (MakeFile) and add the following lines

# These are the flags that gcc requires in order to link correctly against our installed 
# client packages
MYSQL_LIBS := $(shell mysql_config --libs)

right below the Environment block.

Then right click on your project node , select Properties, Build and add $(MYSQL_LIBS) to the Additional options parameter.

like image 29
thanassis Avatar answered Oct 04 '22 02:10

thanassis