C 语言连接 MySQL,使用 gcc 编译时出现链接错误:Undefined symbols for architecture x86_64


操作系统是 OS X 10.9.3 ,安装了 mysql-5.6.20-osx10.8-x86_64.dmg ,默认安装在了 mysql-5.6.20-osx10.8-x86_64 ,并且自动做了软连接: /usr/local/mysql -> mysql-5.6.20-osx10.8-x86_64 ,然后我手动做了软链接: /usr/include/mysql -> /usr/local/mysql/include/ 。

gcc 版本:


 $ gcc -v

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix

version.c 代码:


 #include <stdio.h>
#include "mysql/mysql.h"

int main(int argc, char * argv[])
{
    printf("Mysql client version:%s\n", mysql_get_client_info());
    return 0;
}

编译时出现如下提示:


 $ gcc version.c 

Undefined symbols for architecture x86_64:
  "_mysql_get_client_info", referenced from:
      _main in version-923a36.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

首先请大家原谅我比较笨…… 我从网络上搜索之后,发现这种情况出现的原因很多,有程序内代码拼写错误的,也有 Xcode 添加什么文件的,但是我仅仅是使用 vi 编辑的文件,直接使用 gcc 进行编译,现在还是搞不清楚我这种问题是怎么回事。请大神帮忙解答,感激不尽。

链接 gcc mysql mac c

夜之影◇菰儛 9 years, 8 months ago

此错误是编译的时候没有找到相应的mysql库,你可以用以下方法解决。

终端输入:
gcc version.c -o version $(mysql_config --libs)

说明:
-o version version为output文件.

mysql_config --libs 为安装的mysql库位置和库名字。

然后运行 ./version 即可。

釹紳!綾波麗! answered 9 years, 8 months ago

Your Answer