views:

50

answers:

1

Hi all, when i run the libmemcached example code on my ubuntu, it gave me the error "undefined reference to `memcached_create'", anyone can help ? thanks

#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;

}
+1  A: 

Pass -lmemcached to GCC. This tells it to link to the libmemcached.so shared library.

Matthew Flaschen
Thank you very much.
why
when i run gcc 3gbbn.c -lmemcached, it works. that's to say, my origin problem is: the code found the header file, but the header file cant find the .so file
why
@sharp, yes, the header file only provides the compiler with prototypes. It does not tell the linker where the functions' implementations are.
Matthew Flaschen