{"id":1186,"date":"2022-08-30T15:13:49","date_gmt":"2022-08-30T15:13:49","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/ibmemcached-linking-error-undefined-reference-to-memcached_exist-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:49","modified_gmt":"2022-08-30T15:13:49","slug":"ibmemcached-linking-error-undefined-reference-to-memcached_exist-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/ibmemcached-linking-error-undefined-reference-to-memcached_exist-collection-of-common-programming-errors\/","title":{"rendered":"ibmemcached Linking Error: undefined reference to `memcached_exist&#39;-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to write a sample code using libmemcached c\/c++ client version (0.53)<\/p>\n<pre><code>gcc -o test test.c -I\/home\/libmemcached\/include -L\/home\/libmemcached\/lib -lmemcached -lmemcachedutil\n<\/code><\/pre>\n<p>However i get an error<\/p>\n<blockquote>\n<p>\/tmp\/ccoaToYP.o: In function <code>main': test.c:(.text+0x255): undefined reference to<\/code>memcached_exist&#8217;<\/p>\n<\/blockquote>\n<p>Has anyone come across this issue ? I cannot use version higher than 0.53 (basically any 1.0) due to limitation with installed gcc. I see that this command was added for 0.53.<\/p>\n<p>Also, The path and ld_library_path are straightforward too. <code>PATH<\/code> is set with <code>\/bin:\/sbin:\/usr\/bin:\/usr\/sbin:\/usr\/bin\/X11:\/usr\/sbin<\/code>. <code>LD_LIBRARY_PATH<\/code> is set with <code>\/home\/libmemcached\/lib:\/usr\/lib:\/usr\/lib64:\/lib<\/code><\/p>\n<blockquote>\n<p>$ nm libmemcached.so | grep -i memcached_exist 00014bc2 T _Z15memcached_existP12memcached_stPKcj 00014b06 T _Z22memcached_exist_by_keyP12memcached_stPKcjS2_j $<\/p>\n<\/blockquote>\n<p>If i comment out the memcached_exist call, rest of code compiles and executes just fine.<\/p>\n<pre><code>#include \n#include \n#include \n#include \n\nint main(int argc, char *argv[])\n{\n  memcached_server_st *servers = NULL;\n  memcached_st *memc;\n  memcached_return rc;\n  char *key= \"keystring\";\n  char *value= \"keyvalue\";\n\n  uint32_t flags;\n  char return_key[MEMCACHED_MAX_KEY];\n  size_t return_key_length;\n  char *return_value;\n  size_t return_value_length;\n\n  memc= memcached_create(NULL);\n\n  servers= memcached_server_list_append(servers, \"localhost\", 11211, &amp;rc);\n  rc= memcached_server_push(memc, servers);\n\n  if (rc == MEMCACHED_SUCCESS)\n    fprintf(stderr,\"Added server successfully\\n\");\n  else\n    fprintf(stderr,\"Couldn't add server: %s\\n\",memcached_strerror(memc, rc));\n\n  rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0);\n\n  if (rc == MEMCACHED_SUCCESS)\n    fprintf(stderr,\"Key stored successfully\\n\");\n  else\n    fprintf(stderr,\"Couldn't store key: %s\\n\",memcached_strerror(memc, rc));\n\n  return_value= memcached_get(memc, key, strlen(key), &amp;return_value_length, &amp;flags, &amp;rc);\n  if (rc == MEMCACHED_SUCCESS)\n            {\n              fprintf(stderr,\"Key %s returned %s\\n\",key, return_value);\n            }\n  rc = memcached_exist(memc, key, strlen(key));\n  fprintf(stderr,\" Error Code: %s\\n\",memcached_strerror(memc, rc));\n\n  return 0;\n}\n<\/code><\/pre>\n<p>Thanks Antony<\/p>\n<ol>\n<li>\n<p>If you don&#8217;t want to compile as C++, you can always call the mangled name directly. If you want this code to be reusable and to be able to upgrade the libraries easily, etc, you shouldn&#8217;t do that. For a more extensible solution, I&#8217;ll add to H2CO3&#8217;s answer.<\/p>\n<p>If you want to for some reason keep all your main source compiled as C, you can create a .cpp file that has stubs that call the C++ library functions. For example:<\/p>\n<pre><code>\/\/ libraries.cpp\n\/\/\n\/\/ (includes needed to memcached lib call and types)\nextern \"C\" memcached_return memcached_exist(memcached_st *memc, char *key, size_t len)\n{\n    return memcached_exist(memc, key, len);\n}\n<\/code><\/pre>\n<p>Then you can compile libraries.cpp and link against the memcached libs using g++ to a libraries.o and link against that on your gcc line.<\/p>\n<\/li>\n<li>\n<p>Name mangling. The shared object file contains mangled C++ function (method?) names, while your code is compiled as C, containing the non-mangled name <code>memcached_exist<\/code>. Try compiling your file as C++.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:37:55. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to write a sample code using libmemcached c\/c++ client version (0.53) gcc -o test test.c -I\/home\/libmemcached\/include -L\/home\/libmemcached\/lib -lmemcached -lmemcachedutil However i get an error \/tmp\/ccoaToYP.o: In function main&#8217;: test.c:(.text+0x255): undefined reference tomemcached_exist&#8217; Has anyone come across this issue ? I cannot use version higher than 0.53 (basically any 1.0) due to limitation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1186","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1186"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1186\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}