在MAC OSX上编译简单的mysql c应用程序时,为什么为什么总是得到未定义的符号?
我正在使用MAC OSX并试图编写一个测试" msyql应用程序,该应用程序仅测试与本地数据库的连接:
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));
}
}
我使用以下命令进行编译:gcc -I/usr/local/mysql/include -L/usr/local/mysql/lib mysql.c
I compile using the command: gcc -I/usr/local/mysql/include -L/usr/local/mysql/lib mysql.c
我遇到以下错误
未定义符号
"_mysql_options",引用自:
_main in cceohP7E.o
"_mysql_real_connect",引用自:
_main in cceohP7E.o
"_mysql_init",引用自:
_main in cceohP7E.o
"_mysql_error",引用自:
_main in cceohP7E.o
ld:找不到符号
collect2:ld返回1退出状态
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
我不是C/Unix专家,但我想学习.我知道我在这里运行64位体系结构.这会是一个问题吗?让我知道我在做什么错.
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.
谢谢.
正如其他人所提到的,您缺少该库.幸运的是,MySQL通过命令行的mysql_config
:
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`