macos - C 语言连接 MySQL,使用 gcc 编译时出现链接错误:Undefined symbols for architecture x86_64
大家讲道理
大家讲道理 2017-04-17 11:38:37
[MySQL讨论组]

操作系统是 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 进行编译,现在还是搞不清楚我这种问题是怎么回事。请大神帮忙解答,感激不尽。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(2)
大家讲道理

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

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

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

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

然后运行./version 即可。

PHP中文网

问下如何解决的?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号