-2

I use msgpack-c 1.0.0, when I compile the following program it failed:

#include <msgpack.h>
#include <stdio.h>

int main(void) {

    /* creates buffer and serializer instance. */
    msgpack_sbuffer* buffer = msgpack_sbuffer_new();
    msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

    int j;

    for(j = 0; j<23; j++) {
        /* NB: the buffer needs to be cleared on each iteration */
        msgpack_sbuffer_clear(buffer);

        /* serializes ["Hello", "MessagePack"]. */
        msgpack_pack_array(pk, 3);
        msgpack_pack_str(pk, 5);
        msgpack_pack_str_body(pk, "Hello", 5);
        msgpack_pack_str(pk, 11);
        msgpack_pack_str_body(pk, "MessagePack", 11);
        msgpack_pack_int(pk, j);

        /* deserialize the buffer into msgpack_object instance. */
        /* deserialized object is valid during the msgpack_zone instance alive. */
        msgpack_zone mempool;
        msgpack_zone_init(&mempool, 2048);

        msgpack_object deserialized;
        msgpack_unpack(buffer->data, buffer->size, NULL, &mempool, &deserialized);

        /* print the deserialized object. */
        msgpack_object_print(stdout, deserialized);
        puts("");

        msgpack_zone_destroy(&mempool);
    }

    /* cleaninstrong textg */
    msgpack_sbuffer_free(buffer);
    msgpack_packer_free(pk);
}

the output is:

[biao.zhang@ip-10-18-0-42 ~]$ gcc b.c -Icustom_apps/include/ -L/home/biao.zhang/custom_apps/lib/libmsgpack.a
/tmp/ccZKJsdv.o: In function `main':
b.c:(.text+0x685): undefined reference to `msgpack_zone_init'
b.c:(.text+0x6af): undefined reference to `msgpack_unpack'
b.c:(.text+0x6d8): undefined reference to `msgpack_object_print'
b.c:(.text+0x6ee): undefined reference to `msgpack_zone_destroy'
collect2: error: ld returned 1 exit status

but when I static link the same library with a different way it can compile successfully and run successfully:

[biao.zhang@ip-10-18-0-42 ~]$ gcc b.c -Icustom_apps/include/ -Lcustom_apps/lib -Wl,-Bstatic -lmsgpack -Wl,-Bdynamic
[biao.zhang@ip-10-18-0-42 ~]$ ldd a.out
    linux-vdso.so.1 =>  (0x00007fff892c5000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fef6c221000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fef6c5ef000)

Are there any differences between the two ways of static linking? Any suggestions?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

1 Answers1

1

In gcc

  • -L option is to provide the path to the library.

    (Your first case, it does not actually link with the library. Hence, undefined reference(s)).

  • -l option is to provide the name of the library

    (Your second case, it actually links with the specified library)

Further read: Online gcc help manual for -l and for -L

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • @biaozhang as i mentioned, only providing `-L` does not _include_ the library. You need to use `-l` to link with the library. In your first case, `-l` is not there. – Sourav Ghosh Apr 30 '15 at 09:07
  • thank you so much. I forgot the right way to specify static library. In fact the following command also works well: gcc b.c -Icustom_apps/include/ custom_apps/lib/libmsgpack.a – biao zhang May 04 '15 at 03:18