GCC:在Mac Os上链接dylib

时间:2013-09-03 15:45:56

标签: c macos gcc dylib

如果我没有明确指定其路径,则

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 个答案:

答案 0 :(得分:2)

编译时在最后添加-lmemcached