linux下使用codeblocks编译mysql操作c程序错误

-lpthread
-lrt
-lz

-ldl



1、首先,添加相应的mysql库及头文件路径,库的路径为  /usr/lib/x86_64-linux-gnu/libmysqlclient.a,头文件的路径为/usr/include/mysql.

2、编译出现如下错误:

g++ -L../../../../usr/lib/mysql/plugin -L../../../../usr/lib/x86_64-linux-gnu -L/usr/lib/mysql/plugin -o bin/Debug/TestMySql obj/Debug/main.o   ../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a /usr/lib/x86_64-linux-gnu/libmysqlclient.a
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `add_plugin':
(.text+0x1ed): undefined reference to `dlclose'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_client_plugin_deinit':
(.text+0x28b): undefined reference to `dlclose'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x51e): undefined reference to `dlopen'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x539): undefined reference to `dlsym'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x61a): undefined reference to `dlerror'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x62e): undefined reference to `dlclose'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_reinit':
(.text+0x343): undefined reference to `pthread_getspecific'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_end':
(.text+0x5d6): undefined reference to `pthread_key_delete'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_end':
(.text+0x5e2): undefined reference to `pthread_mutexattr_destroy'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_end':
(.text+0x5ee): undefined reference to `pthread_mutexattr_destroy'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_init':
(.text+0x942): undefined reference to `pthread_getspecific'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_init':
(.text+0x974): undefined reference to `pthread_setspecific'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_init':
(.text+0xb42): undefined reference to `pthread_mutexattr_init'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_init':
(.text+0xb4f): undefined reference to `pthread_mutexattr_settype'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_init':
(.text+0xb5e): undefined reference to `pthread_mutexattr_init'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_init':
(.text+0xb6b): undefined reference to `pthread_mutexattr_settype'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_init':
(.text+0xb79): undefined reference to `pthread_key_create'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_init':
(.text+0xcd5): undefined reference to `pthread_create'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_global_init':
(.text+0xf66): undefined reference to `pthread_join'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_end':
(.text+0xfa3): undefined reference to `pthread_getspecific'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `my_thread_end':
(.text+0xfd5): undefined reference to `pthread_setspecific'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_thr_init.c.o): In function `_my_thread_var':
(.text+0x113a): undefined reference to `pthread_getspecific'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(charset.c.o): In function `get_collation_number':
(.text+0xfd8): undefined reference to `pthread_once'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(charset.c.o): In function `get_charset_number':
(.text+0x1078): undefined reference to `pthread_once'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(charset.c.o): In function `get_charset_name':
(.text+0x1102): undefined reference to `pthread_once'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(charset.c.o): In function `get_charset':
(.text+0x11e1): undefined reference to `pthread_once'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(charset.c.o): In function `get_charset_by_name':
(.text+0x12ae): undefined reference to `pthread_once'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(charset.c.o):(.text+0x13a0): more undefined references to `pthread_once' follow
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_compress.c.o): In function `my_compress_alloc':
(.text+0x7b): undefined reference to `compress'
../../../../usr/lib/x86_64-linux-gnu/libmysqlclient.a(my_compress.c.o): In function `my_uncompress':
(.text+0x1f5): undefined reference to `uncompress'
collect2: ld returned 1 exit status

网上查了好久,直接使用gcc 编译没报错误,怀疑是codeblocks配置问题,找了网上资料发现是在链接时需要加入相应的库:

-lpthread
-lrt
-lz
-ldl

linux下使用codeblocks编译mysql操作c程序错误_第1张图片

再次编译,运行成功。


程序源码如下:

#include 
#include 
#include 

int main()
{
    MYSQL my_con;
    MYSQL_RES *my_res;
    MYSQL_ROW my_row;
    MYSQL_FIELD *field;
    int rows,res,column;
    int i, j;
    mysql_init(&my_con);
    if(mysql_real_connect(&my_con,"localhost","root","myscript","mysql",0,NULL,0) == NULL)
    {
        printf("link databases error!\n");
        exit(1);
    }
    mysql_query(&my_con,"set names utf-8");
    if(mysql_query(&my_con,"select * from user"))
    {
        printf("query error!\n");
        exit(1);
    }
    my_res = mysql_store_result(&my_con);
    while(my_row = mysql_fetch_row(my_res))
    {
      printf("%s   %s\n", my_row[1], my_row[2]);
    }
    mysql_free_result(my_res);
    mysql_close(&my_con);

}






你可能感兴趣的:(编程中的错误,敏捷技能修炼)