Cannot compile C devguide-examples: connecting.c:8: undefined reference to `lcb_strerror' etc

Hi, I just attended @mattispasch SoCal Code Camp and was hoping to follow allow and implement a Python/C Couchbase Lite client. After installed the CE server and C SDK using the http://packages.couchbase.com/clients/c/couchbase-csdk-setup Perl script on Ubuntu 12.04 64-bit I ran into the following compile time errors. He suggested I ask here to see if anyone had the same issue.

$ make
cc  -Wall -g -lcouchbase  connecting.c   -o connecting
/tmp/ccFsK0V3.o: In function `die':
/home/user/devguide-examples/c/connecting.c:8: undefined reference to `lcb_strerror'
/tmp/ccFsK0V3.o: In function `main':
/home/user/devguide-examples/c/connecting.c:23: undefined reference to `lcb_create'
/home/user/devguide-examples/c/connecting.c:28: undefined reference to `lcb_connect'
/home/user/devguide-examples/c/connecting.c:34: undefined reference to `lcb_wait'
/home/user/devguide-examples/c/connecting.c:37: undefined reference to `lcb_get_bootstrap_status'
/home/user/devguide-examples/c/connecting.c:41: undefined reference to `lcb_get_num_nodes'
/home/user/devguide-examples/c/connecting.c:45: undefined reference to `lcb_destroy'
collect2: ld returned 1 exit status
make: *** [connecting] Error 1

Headers are installed.

$ ls /usr/include/libcouchbase/
api3.h          configuration.h  error.h  libevent_io_opts.h  pktfwd.h          vbucket.h
assert.h        couchbase.h      http.h   libev_io_opts.h     plugins           views.h
cntl.h          _cxxwrap.h       iops.h   libuv_io_opts.h     select_io_opts.h  visibility.h
cntl-private.h  deprecated.h     kvbuf.h  n1ql.h              sysdefs.h

Thanks!

You need to add -lcouchbase after the source/object file, not before. This is because when it is specified before the object file, the linker will decide that it doesn’t need any of its symbols (since none of the objects passed before the library require them).

In your Makefile, you simply need to set LDFLAGS to -lcouchbase and all will be well, for example

LDFLAGS=`-lcouchbase`

all: connecting

Thanks @mnunberg! That was the issue.

I found it easier to change LDFLAGS to LDLIBS in the Couchbase example Makefile. LDLIBS tells make to put the libraries after the object files. See pull request #2

From the GNU Make documentation:

LDLIBS
Library flags or names given to compilers when they are supposed to invoke the linker, ‘ld’. Non-library linker flags, such as -L, should go in the LDFLAGS variable.

LDFLAGS
Extra flags to give to compilers when they are supposed to invoke the linker, ‘ld’, such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead.