GCC:在Mac OS上链接dylib

4

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

显然在这种情况下找不到库。我做错了什么?

1个回答

3
在编译时,请在最后添加-lmemcached

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接