您的位置:首页 > 移动开发 > Objective-C

error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such

2012-05-23 15:59 399 查看
今天写了个简单的mysql程序,通过C语言。
示例代码


View Code

#include "stdio.h"
#include "mysql.h"
#include <pthread.h>
#include "stdlib.h"
#include "string"
#include "iostream"

usingnamespace std;

typedef struct SqlInfo
{
string server;
string user;
string passwd;
string db;
}SqlInfo;

void* showdatabase(void* arg)
{
SqlInfo *p = (SqlInfo*)arg;

MYSQL *mysql_test;
MYSQL_RES *ret;
MYSQL_ROW row;

// char *server = "localhost";
// char *user = "root";
// char *passwd = "1234"; //伪密码
// char *db = "mysql";

mysql_test = mysql_init(NULL);
if (!mysql_real_connect(mysql_test, p->server.c_str(), p->user.c_str(), p->passwd.c_str(), p->db.c_str(), 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(mysql_test));
return NULL;
}

if (mysql_query(mysql_test, "show databases"))
{
fprintf(stderr, "%s\n", mysql_error(mysql_test));
return NULL;
}
ret = mysql_use_result(mysql_test);

printf("Show Databases:\n");
// while((row = mysql_fetch_row(ret)) != NULL)
// {
// printf("%s\n", row[0]);
// }

//equal to the below
unsigned int num_fields;
unsigned int i;

num_fields = mysql_num_fields(ret);
while ((row = mysql_fetch_row(ret)))
{
unsigned long*lengths;
lengths = mysql_fetch_lengths(ret);
for(i =0; i < num_fields; i++)
{
printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL");
}
printf("\n");
}

mysql_free_result(ret);
mysql_close(mysql_test);
}

int main(int argc, char* argv[])
{
if (argc <5)
{
cout <<"Usage: app server user passwd db"<< endl;
return-1;
}

SqlInfo my_info;
my_info.server = argv[1];
my_info.user = argv[2];
my_info.passwd = argv[3];
my_info.db = argv[4];

pthread_t thread1;

pthread_create(&thread1, NULL, showdatabase, (void*)&my_info);
pthread_join(thread1,NULL);

return0;
}


1、遇到的问题
1)error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory
再网上下载了一个so文件。
//locate libmysqlclient.so.16
复制文件到路径:
/usr/local/mysql/lib/mysql/libmysqlclient.so.16
修改文件:
vi /etc/ld.so.conf
增加一行 /usr/local/mysql/lib/mysql
然后执行ldconfig生效。
2、关于C语言中调用mysql API
gcc -o showdata $(mysql_config --cflags) showdata.cpp $(mysql_config --libs)
mysql_config为MySQL中一个特殊的脚本,为编译MySQL客户端,并连接到MySQL服务器提供有用的信息。【3】
当然也要吧在makefile中指定如上选项,或
INC+=-I/usr/include/mysql

LIB=-lmysqlclient -lpthread -lssl

附:
mysql的安装:【4,5】
要注意的一点是,安装的mysql版本要与操作系统版本匹配。
安装完后,通过以下命令设置密码:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h love password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation

You can start the MySQL daemon with:

rcmysql start

You can test the MySQL daemon with mysql-test package

参考
【1】
http://download.csdn.net/source/3528499
【2】
http://blog.sina.com.cn/s/blog_4ab24dd50100wnkv.html
【3】
http://blog.sina.com.cn/s/blog_59d470310100i16r.html
【4】
http://www.iteye.com/topic/647152
【5】
http://www.mysql.com/downloads/mysql/#downloads
【6】 http://wenku.baidu.com/view/5e093ddc5022aaea998f0fbc.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐