包含后找不到sql函数

时间:2017-12-06 02:28:34

标签: c++ mysql c macos

我一直在使用MacOSX上的c基本服务器/客户端程序,当我试图将服务器链接到mysql数据库时,我遇到了严重的问题。我检查了sql头文件,它有编译器找不到的所有函数声明。我下载了一个连接器,专门用于x86_64架构,并使用它给出了同样的错误。

请考虑以下代码:

#include <mysql/mysql.h>
#include <mysql/my_global.h>

int main(int argc, char const *argv[]) {

   //Create the connector object
   MYSQL *con = mysql_init(NULL);
   if (con == NULL) {
     fprintf(stderr, "%s\n", mysql_error(con));
     exit(1);
   }

   //Connect to database
   if (mysql_real_connect(con, "localhost", "username","password", NULL, 0, NULL, 0) == NULL) {
     fprintf(stderr, "%s\n", mysql_error(con));
     mysql_close(con);
     exit(1);
   }
}  

编译时出现以下错误:

Undefined symbols for architecture x86_64:
   "_finish_with_error", referenced from:
   _main in ServerSocket-dbb1df.o
   "_mysql_close", referenced from:
   _main in ServerSocket-dbb1df.o
   "_mysql_error", referenced from:
   _main in ServerSocket-dbb1df.o
   "_mysql_init", referenced from:
   _main in ServerSocket-dbb1df.o
   "_mysql_query", referenced from:
   _main in ServerSocket-dbb1df.o
   "_mysql_real_connect", referenced from:
   _main in ServerSocket-dbb1df.o
   "_mysql_store_result", referenced from:
   _main in ServerSocket-dbb1df.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

任何有关此处出错的帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

MySQL文档,特别是this page,解释了如何将程序与MySQL链接。这是从上面的链接复制粘贴的:

mysql_config显示编译或链接所需的选项:

shell> mysql_config --cflags
shell> mysql_config --libs

您可以运行这些命令以获取正确的选项,并手动将它们添加到编译或链接命令。或者,使用反引号直接在命令行中包含mysql_config的输出:

shell> gcc -c `mysql_config --cflags` progname.c
shell> gcc -o progname progname.o `mysql_config --libs`

如果您使用C ++(在问题中同时使用C和C ++作为标记),您当然希望将gcc替换为g++,同时将--cflags替换为{ {1}}。