2010-09-19 36 views
0

當我在我的ubuntu上運行libmemcached示例代碼時,它給了我錯誤「未定義引用memcached_create」,任何人都可以提供幫助嗎?謝謝libmemcached問題

#include <libmemcached/memcached.h> 

int main(int argc, char **argv) { 
    //memcached_servers_parse (char *server_strings); 
    memcached_server_st *servers = NULL; 
    memcached_st *memc; 
    memcached_return rc; 
    char *key= "keystring"; 
    char *value= "keyvalue"; 

    memc= memcached_create(NULL); 
    servers= memcached_server_list_append(servers, "localhost", 11211, &rc); rc= memcached_server_push(memc, servers); 

    if (rc == MEMCACHED_SUCCESS) 
    fprintf(stderr,"Added server successfully\n"); else 
    fprintf(stderr,"Couldn't add server: %s\n",memcached_strerror(memc, rc)); 

    rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0); 

    if (rc == MEMCACHED_SUCCESS) 
    fprintf(stderr,"Key stored successfully\n"); else 
    fprintf(stderr,"Couldn't store key: %s\n",memcached_strerror(memc, rc)); 

    return 0; 

} 

回答

3

通過-lmemcached GCC。這告訴它鏈接到libmemcached.so共享庫。

+0

非常感謝。 – why 2010-09-19 01:36:21

+0

當我運行gcc 3gbbn.c -lmemcached,它的工作原理。也就是說,我的起源問題是:代碼找到頭文件,但頭文件不能找到.so文件 – why 2010-09-19 01:38:21

+0

@sharp,是的,頭文件只給編譯器提供了原型。它沒有告訴鏈接器函數的實現在哪裏。 – 2010-09-19 02:05:27

0

當我建庫(libmemcached),它只能建立靜態庫,當我連接我的程序(如上所述),它給了我

$ gcc MyMemClient.c -o MyMemClient -L/usr/local/lib/libmemcached.a 
/tmp/ccQEXVjv.o:MyMemClient.c:(.text+0x1f): undefined reference to `_memcached_create' 
/tmp/ccQEXVjv.o:MyMemClient.c:(.text+0x58): undefined reference to `_memcached_server_list_append' 
//ccQEXVjv.o:MyMemClient.c:(text+0x6d): undefined reference to `_memcached_server_push' 
/tmp/ccQEXVjv.o:MyMemClient.c:(.text+0xc1): undefined reference to `_memcached_strerror' 
/tmpccQEXVjv.o:MyMemClient.c:(.text+0x12a): undefined reference to `_memcached_set' 
/tmp/ccQEXVjv.o:MyMemClient.c:(.text+0x170): undefined reference to `_memcached_strerror' 
/tmp/ccQEXVjv.o:MyMemClient.c:(.text+0x199): undefined reference to `_memcached_server_free' 
//ccQEXVjv.o:MyMemClient.c:(.text+0x1a4): undefined reference to `_memcached_free' 
ld returned 1 exit status 
0
  1. 打開你的終端,將目錄更改你的libmemcached是未壓縮的。
  2. ./configure
  3. make
  4. 在C項目
  5. ,點擊右鍵並選擇 「屬性」。在「鏈接器」中添加「libmemcached.so」庫,並記住添加庫目錄(指向包含libmemcached.so的目錄的鏈接)。