Hi guys. I am trying to write a simple c++ program using the g++ compiler in linux (debian) that can access a mySQL database, however I am getting some frustrating error messages that I do not understand.
I have included the mysql library header file ('#include <mysql/mysql.h>') and have attempted to call several functions from this api but I get the error 'undefined reference to <function name>' for any function that I try and use...
Eg:
1 2 3 4 5
main.cpp:(.text+0xfa): undefined reference to `mysql_init'
main.cpp:(.text+0x111): undefined reference to `mysql_error'
main.cpp:(.text+0x167): undefined reference to `mysql_real_connect'
main.cpp:(.text+0x17e): undefined reference to `mysql_error'
main.cpp:(.text+0x19c): undefined reference to `mysql_close'
my make file contains the following (nothing fancy):
1 2 3
all: run
run: main.cpp
g++ main.cpp -o run
I have used external libraries before, like direct X (in windows), and the STL, but I have never seen this error before, and as far as I am aware, if a file has a function in it I want to use, I can include that file and then use the function.
So I guess what I'm asking is for some help understanding why it wont let me access any of the functions contained in /mysql/mysql.h?
It looks like you need to add a library to your make file. I've never linked agains MySQL libraries, so I'm not much help. Perhaps there are some example makefiles under the directory where MySQL is installed.
It links against libmysql.so (in case of dynamic linking, which is the case here, otherwise against libmysql.a etc.) as found in the library search paths. Basically -l[something] will link to [library search path]/lib[something].so .
With -L, you add another path to the list of library search paths but I guess that isn't necessary since the libs should be in /usr/lib.
And no, this doesn't link any code from /usr/include/mysql at all, it links to existing binary files but you have (I expect so) included headers from /usr/include/mysql in your code using the #include direction. The included headers contain the definitions of the functions linked from libmysqlclient.so so the compiler knows how they're used in your program.
You can use the command ldd binaryName to see what libraries your program is linked against and where they were found.