c++ - include后找不到sql函数

标签 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)

任何关于这里出了什么问题的帮助将不胜感激!

最佳答案

错误消息“ld:未找到符号”表示您没有将库链接到程序中。

MySQL 文档,特别是 this page ,解释了如何将您的程序与 MySQL 库链接:

mysql_config displays the options needed for compiling or linking:

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

You can run those commands to get the proper options and add them manually to compilation or link commands. Alternatively, include the output from mysql_config directly within command lines using backticks:

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

如果您使用 C++(您的问题中同时包含 C 和 C++ 作为标签),您当然会想要将 gcc 替换为 g++,但也要替换 --cflags--cxxflags

关于c++ - include后找不到sql函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47665882/

相关文章:

c++ - 我有一个浮点溢出问题

c++ - 使用 Visual studio 2013 为 native DLL 正确生成 PDB 文件

mysql - 如何为博客系统设计数据库?

c - C 中字符和字符串函数的二维数组

c++ - 静态回调函数和非静态成员

c++ - 在 Ubuntu 上使用 libc++ 的 clang 编译器出现问题

mysql - MySql 的排名结果

php - 使用格式化的键和值将所有 MYSQL 记录传递到数组中

c - 从文件中读取给定位置的字符数 - C

c++ - 进程正在等待另一个进程终止时的信号处理