Mac上c语言连接mysql遇到的问题

  参照《Beginning Linux Programming》上的例程写了一个连接mysql的c语言小程序connect1.c。但是按照书上的编译命令无法编译。然后经过查阅资料解决了问题。记录如下:

首先书上的编译命令是:

gcc -I /usr/local/mysql/include/ connect1.c  -L /usr/local/mysql/lib/ -l mysqlclient -o connect1

编译不成功,始终提示directory not found。网上有人说编译器不能识别全路径制定的lib库什么的,众说纷纭。后来我尝试调换了一下命令各参数的顺序:

gcc -I /usr/local/mysql/include/ -L /usr/local/mysql/lib/ connect1.c -o connect1 -lmysqlclient

妈蛋就成功了。编译成功,无法运行,提示不能加载mysql库,这好像是mysql安装包的问题,就是安装没成功,编译器找不到对应的库。

dyld: Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/tmxu/Projects/BeginningLinuxProgramming/./connect1
  Reason: image not found
Trace/BPT trap: 5

我参照了这个博客的解决方案http://www.blog.bridgeutopiaweb.com/post/how-to-fix-mysql-load-issues-on-mac-os-x/

评论第一楼的解决方案好评最高,于是执行一下,我的问题也解决了。

Thank you for sharing ideas about this problem! For me none of the things above worked, unfortunately. However, I simply added a softlink from the standard MAC OS X dyld-library-path (/usr/lib/) to the libmysqlclient.18.dylib... so it can be found easily without changing much of the system. Maybe a good install process of MySQL should have done that automatically.
Here is the command:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

 

 

posted @ 2013-07-06 15:28 明之道 阅读(...) 评论(...) 编辑 收藏