对“memcached_exist”的未定义引用

undefined reference to `memcached_exist'

我使用的是 C 而不是 C++

我正在尝试在 C 中使用 memcached_exist,但出现此错误:

undefined reference to `memcached_exist'

这是我的代码:

  #include <stdio.h>
  #include <string.h>
  #include <unistd.h>
  #include <libmemcached/memcached.h>


  int main(int argc, char *argv[])  {

   memcached_server_st *servers = NULL;
    memcached_st *memc;
    memcached_return rc;
    char *key= "keystring";
    char *value= "keyvalue";

  //  memcached_server_st *memcached_servers_pars (char *server_strings);
    memc= memcached_create(NULL);

    servers= memcached_server_list_append(servers, "localhost", 11211, &rc);
    rc= memcached_server_push(memc, servers);
rc= memcached_set(memc, key, strlen(key), value, strlen(value), expire, flag);                                                                           
  rc = memcached_exist(memc, key, strlen(key));

我正在编译:-lmemcached -lmemcachedutil

此解决方案无效,因为我使用的是 C:

ibmemcached Linking Error: undefined reference to `memcached_exist'

请注意,为了提供简短的答案,我需要一个 mcve,包括 您用来编译 的完整命令,因为这需要一些超出基本编译的额外开关。然而,这个问题有一个决定性的答案……你已经给了我们,顺便说一下,但我们稍后会谈到。首先,让我们介绍一下更常见的原因。

这是一个链接器错误。链接器错误通常属于以下三类之一:

  • compilation/linking 阶段缺少库。 The documentation 表示您已链接到正确的图书馆。
  • 链接顺序! 在这种情况下,-lmemcached -lmemcachedutil 的位置很重要。如果您的命令符合 cc -lmemcached file.c,这就是您的(至少部分)错误。它需要看起来更像:cc file.c -lmemcached...
  • 印刷错误。基于 the documentation and in spite of the question you linked to, it would initially seem like this isn't your case. However, if you inspect the question you linked to more closely then you'll notice that that wasn't using C++, either! Perhaps your question is a duplicate of that question, with the exception of course that you haven't given anywhere near as much information as that question. A quick search for the source code indicates that this function (memcached_exist) is compiled from a file named exist.cc, using... you guessed it: A C++ compiler. So considering the name mangling that will occur (especially because the actual function isn't defined using extern "C"), you've got a typographic error, whether you're willing to accept it or not. Have you observed the presense (or lack) of name mangling, using the command that's in the question you linked to (nm libmemcached.so | grep -i memcached_exist)? If you can see the name mangling, perhaps you should try the answer to the question you linked to, anyway...