3

gcc如果我没有明确指定其路径,则无法找到动态库。首先,我使用的libmemcached是我安装的brew.

17:27:14 shell% ls -la /usr/local/lib/libmemcached*
lrwxr-xr-x  1 user  wheel  55 Sep  2 12:42 /usr/local/lib/libmemcached.11.dylib -> ../Cellar/libmemcached/1.0.17/lib/libmemcached.11.dylib
lrwxr-xr-x  1 user  wheel  48 Sep  2 12:42 /usr/local/lib/libmemcached.a -> ../Cellar/libmemcached/1.0.17/lib/libmemcached.a
lrwxr-xr-x  1 user  wheel  52 Sep  2 12:42 /usr/local/lib/libmemcached.dylib -> ../Cellar/libmemcached/1.0.17/lib/libmemcached.dylib
lrwxr-xr-x  1 user  wheel  58 Sep  2 12:42 /usr/local/lib/libmemcachedutil.2.dylib -> ../Cellar/libmemcached/1.0.17/lib/libmemcachedutil.2.dylib
lrwxr-xr-x  1 user  wheel  52 Sep  2 12:42 /usr/local/lib/libmemcachedutil.a -> ../Cellar/libmemcached/1.0.17/lib/libmemcachedutil.a
lrwxr-xr-x  1 user  wheel  56 Sep  2 12:42 /usr/local/lib/libmemcachedutil.dylib -> ../Cellar/libmemcached/1.0.17/lib/libmemcachedutil.dylib

我的hellomemcached.c样子是这样的:

#include <libmemcached/memcached.h>

int main ()
{
    memcached_return_t  rc;
    memcached_server_st*    servers = NULL;
    memcached_st*       memcached;

    // initialize the memcached structure
    memcached = memcached_create(NULL);
    if (!memcached)
        return 0;
}

使用以下命令编译以成功结束:

gcc -arch x86_64 /usr/local/lib/libmemcached.dylib -I/usr/local/include -o hellomemcached hellomemcached.c

但是,如果我尝试使用包含库的文件夹的路径来编译它:

gcc -arch x86_64 -L/usr/local/lib -I/usr/local/include -o hellomemcached hellomemcached.c

我收到一个错误:

Undefined symbols for architecture x86_64:
    "_memcached_create", referenced from:
    _main in ccYCwHa6.o
ld: symbol(s) not found for architecture x86_64

显然在这种情况下它找不到图书馆。我做错了什么?

4

1 回答 1

2

-lmemcached编译时在最后添加。

于 2013-09-03T16:04:54.763 回答