gpt4 book ai didi

c - GCC:在 Mac Os 上链接 dylib

转载 作者:行者123 更新时间:2023-12-01 06:20:23 24 4
gpt4 key购买 nike

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

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

最佳答案

添加 -lmemcached在编译的最后。

关于c - GCC:在 Mac Os 上链接 dylib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18596398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com