I'm using MAC OSX and trying to write a 'test' msyql app that just tests the connection to a local database:
#include <my_global.h>
#include <mysql.h>
int main(void)
{
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "examplemysql");
if(!mysql_real_connect(&mysql, "localhost", "someuser", "somepassword", "sampledb", 0, NULL, 0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&mysql));
}
}
I compile using the command: g开发者_StackOverflow社区cc -I/usr/local/mysql/include -L/usr/local/mysql/lib mysql.c
I get the following errors
Undefined symbols
"_mysql_options", referenced from: _main in cceohP7E.o "_mysql_real_connect", referenced from: _main in cceohP7E.o "_mysql_init", referenced from: _main in cceohP7E.o "_mysql_error", referenced from: _main in cceohP7E.o ld: symbol(s) not found collect2: ld returned 1 exit status
I'm no C/Unix expert, but I want to learn. I know I have a 64-bit architecture running here. Can this be an issue? Let me know what I'm doing wrong.
Thanks.
As the others have mentioned, you're missing the library. Fortunately, MySQL makes this easy via the command line's mysql_config
:
gcc `mysql_config --include` mysql.c `mysql_config --libs`
You need to link in the shared library libmysql.so
, those are linker errors with trying to find other symbols.
Something like this...
gcc -I/usr/local/mysql/include -L/usr/local/mysql/lib mysql.c -lmysql
I'm updating this answer for OSX Lion and for anyone searching for this... just because it was difficult for me.
The mysql_config is one option, or specifying the gcc flags yourself is another but without the right C connector, it all fails.
I'm on Lion 10.7.4 Macbook air and I downloaded mysql-connector-c-6.0.2-osx10.5-x86-64bit.tar which I got from http://ftp.dc.aleron.net/pub/mysql/Connector-C/
untar and unzip all the files and copy the respective /bin /include and /lib files to the respective /usr/local/bin, /usr/local/include and /usr/lical/lib directories as root
then I used gcc -o mysqlapp mysqlapp.c -L /usr/local/lib -l mysqlclient -lz -I /usr/local/include
mysqlapp.c uses mysql.h and everything compiles and runs fine. The connector I originally downloaded from www.mysql.com would not work correctly. I'm not sure what happened with that one.
精彩评论