ibmemcached链接错误:未定义参考`memcached_exist“ [英] ibmemcached Linking Error: undefined reference to `memcached_exist'

查看:587
本文介绍了ibmemcached链接错误:未定义参考`memcached_exist“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用libmemcached C / C ++客户端版本写一个样本code(0.53)

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:在功能主要:
  。test.c以:(文字+ 0x255):未定义引用
memcached_exist

/tmp/ccoaToYP.o: In function main': test.c:(.text+0x255): undefined reference tomemcached_exist'

有没有人碰到这个问题?我不能用版本比0.53高(基本上任何1.0)由于安装GCC限制。我看到,0.53添加此命令。

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.

此外,路径和LD_LIBRARY_PATH很简单了。
PATH 设置为 /斌:/ sbin目录:在/ usr / bin中:/ usr / sbin目录:在/ usr / bin中/ X11:/ usr / sbin目录
LD_LIBRARY_PATH 设置为 /家庭/ libmemcached / lib目录下:/ usr / lib目录:在/ usr / lib64目录:/ lib目录

Also, The path and ld_library_path are straightforward too. PATH is set with /bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/sbin. LD_LIBRARY_PATH is set with /home/libmemcached/lib:/usr/lib:/usr/lib64:/lib

$纳米libmemcached.so | grep的-i memcached_exist
  00014bc2ŧ_Z15memcached_existP12memcached_stPKcj
  00014b06ŧ_Z22memcached_exist_by_keyP12memcached_stPKcjS2_j
  $

$ nm libmemcached.so | grep -i memcached_exist 00014bc2 T _Z15memcached_existP12memcached_stPKcj 00014b06 T _Z22memcached_exist_by_keyP12memcached_stPKcjS2_j $

如果我注释掉memcached_exist电话,code的休息编译和执行就好了。

If i comment out the memcached_exist call, rest of code compiles and executes just fine.

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

  uint32_t flags;
  char return_key[MEMCACHED_MAX_KEY];
  size_t return_key_length;
  char *return_value;
  size_t return_value_length;

  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_value= memcached_get(memc, key, strlen(key), &return_value_length, &flags, &rc);
  if (rc == MEMCACHED_SUCCESS)
            {
              fprintf(stderr,"Key %s returned %s\n",key, return_value);
            }
  rc = memcached_exist(memc, key, strlen(key));
  fprintf(stderr," Error Code: %s\n",memcached_strerror(memc, rc));

  return 0;
}

谢谢
安东尼

Thanks Antony

推荐答案

如果你不想编译为C ++,你总是可以直接调用重整名称。如果你想要这个code受到重用,并能够轻松升级库,等等,你不应该这样做。对于更扩展的解决方案,我会添加到H2CO3的答案。

If you don'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't do that. For a more extensible solution, I'll add to H2CO3's answer.

如果您想为某些原因保持编译为C所有的主要来源,你可以创建具有调用C ++库函数存根.cpp文件。例如:

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:

// libraries.cpp
//
// (includes needed to memcached lib call and types)
extern "C" memcached_return memcached_exist(memcached_st *memc, char *key, size_t len)
{
    return memcached_exist(memc, key, len);
}

然后你可以使用G ++在您的gcc行libraries.o和纽带针对编译libraries.cpp和链路对memcached的库。

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.

这篇关于ibmemcached链接错误:未定义参考`memcached_exist“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆