Gcc does not compile or 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 run this test program from the terminal but will receive the following error message:

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

what's wrong? my ubuntu system

+7
c gcc undefined-reference mysql
source share
7 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 the MySQL database server.

Pass --libs option - Libraries and parameters needed to communicate 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 compilation / link line:

 gcc -o mysql-test MySQL-Test.c $(mysql_config --libs) 
+14
source share

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

Replace -L/usr/local/mysql/lib wherever the client library is (if it is not already in your libpath)

See MySql instructions for creating clients .

+4
source share

To use Netbeans on Linux

Open make (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 under the Environment block.

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

+3
source share

You do not contact libraries. Use: gcc -llibrarygoeshere -o mysql-test MySQL-Test.c See here for more information on communicating with gcc.

+2
source share

This is not a compilation error. This is a communication error.

Add a mysql library to create your executable with the -lmysql option should do the trick.

+2
source share

You forgot to link yourself to the MySQL library. Try adding -lmysql to your compilation line.

See http://www.adp-gmbh.ch/cpp/gcc/create_lib.html for more details.

+2
source share

Maybe late, but worked for me
If you are using an IDE, you must link the library to your project.
I am using CodeBlocks on ubuntu 12.4 64x. To link the library, you must go to Project -> Build options -> linker settings and add the library. this is my lib path: /usr/lib/x86_64-linux-gnu/libmysqlclient.so

Hope to be helpful ...

+1
source share

All Articles