gcc wont compile and run MySQL C libraries
Asked Answered
E

7

8
#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

Euterpe answered 3/8, 2010 at 11:53 Comment(0)
F
17

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)
Flori answered 3/8, 2010 at 12:0 Comment(3)
thanks that script helped clear up afew things, the following works just fine: "gcc -o mysql-test MySQL-Test.c -lmysqlclient" now that the test class is working, how would i port that command to an IDE such as NetBeans?Euterpe
I'd suggest writing a Makefile which will take care of compiling the application automatically. In there you can specify the command line arguments for GCC. You can find information on how to write makefiles here: delorie.com/djgpp/doc/ug/larger/makefiles.html There are load of websites to help you writing your own makefile.Kho
@Flori How would you do this in windows?Documentation
S
4

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.

Skinned answered 3/8, 2010 at 11:56 Comment(0)
L
3

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.

Liris answered 21/8, 2014 at 7:53 Comment(0)
H
2

You are not linking to the libraries. Use: gcc -llibrarygoeshere -o mysql-test MySQL-Test.c See here for more information about linking with gcc.

Hamlett answered 3/8, 2010 at 11:55 Comment(0)
S
2

It is not a compilation error. It is a link error.

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

Sig answered 3/8, 2010 at 11:56 Comment(0)
K
2

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

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

Kho answered 3/8, 2010 at 11:57 Comment(0)
F
1

Maybe late but worked for me
If you are using an IDE you should link the library to your project.
I am using CodeBlocks on ubuntu 12.4 64x. For linking the library, you should 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 be useful...

Flocculate answered 4/11, 2013 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.