質問

gcc is unable to find a dynamic library if I don't specify its path explicitly. First of all I'm using libmemcached which I've got installed with 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

My hellomemcached.c looks like this:

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

The compilation with the following command ends with success:

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

But if I try to compile it with a path to the folder, which contains the library:

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

I get an error:

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

Apparently it can't find the library in that case. What I'm doing wrong?

役に立ちましたか?

解決

Add -lmemcached at the very end when compiling.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top