msgpack-c icon indicating copy to clipboard operation
msgpack-c copied to clipboard

Cannot link project using msgpack-c library

Open marco711 opened this issue 3 years ago • 10 comments

Hello, I'm using the msgpack-c library in my project. I've been working with the msgpack_pack_str_body and msgpack_pack_int16 functions without problem. Now I want to use the msgpack_unpack_next and msgpack_object_print functions as in the "Simple program with a loop" but I get the following compilation error:

[5/7] Linking CXX executable mqtt_tcp.elf
FAILED: mqtt_tcp.elf 
.
.
.
b/gcc/riscv32-esp-elf/8.4.0/../../../../riscv32-esp-elf/bin/ld: esp-idf/main/libmain.a(message_pack_functions.c.obj): in function `messagepack_encoding':
/home/esp/IoTlift/mqtt-tcp/build/../main/message_pack_functions.c:77: undefined reference to `msgpack_unpack_next'
/home/.espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/../lib/gcc/riscv32-esp-elf/8.4.0/../../../../riscv32-esp-elf/bin/ld: /home/esp/IoTlift/mqtt-tcp/build/../main/message_pack_functions.c:81: undefined reference to `msgpack_object_print'
/home/.espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/../lib/gcc/riscv32-esp-elf/8.4.0/../../../../riscv32-esp-elf/bin/ld: esp-idf/main/libmain.a(message_pack_functions.c.obj): in function `msgpack_unpacked_destroy':
/home/esp/IoTlift/mqtt-tcp/build/../msgpack-c/msgpack-c-c_master/include/msgpack/unpack.h:260: undefined reference to `msgpack_zone_free'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1

Nevertheless, the library is included in the project and msgpack_zone_free is in "zone.h" file which is included in "unpack.h" file. How can I solve this problem?

marco711 avatar Aug 24 '22 10:08 marco711

msgpack_pack_str_body is an inline function. https://github.com/msgpack/msgpack-c/blob/c3df1bb26ebdd01d618ecca7ae2d6b4e37d5abd7/cmake/pack_template.h.in#L782-L785

msgpack_zone_free is not an inline function. https://github.com/msgpack/msgpack-c/blob/c3df1bb26ebdd01d618ecca7ae2d6b4e37d5abd7/src/zone.c#L217-L222

So you need to link the library libmsgpackc.a.

redboltz avatar Aug 24 '22 10:08 redboltz

Thank you. How do I link this library?

marco711 avatar Aug 24 '22 11:08 marco711

See your compiler/linker manual. Usualy -lmsgpackc.

redboltz avatar Aug 24 '22 11:08 redboltz

Hello, I'm using ESP-IDF which uses CMake and Ninja build. I've been reviewing the *cmake and CMakeLists.txt files in order to try to link the library libmsgpackc.a but it is not clear for me how to do the linking.

marco711 avatar Aug 25 '22 10:08 marco711

Here are example source files and CMakeLists.txt. The following line might help you. https://github.com/msgpack/msgpack-c/blob/c_master/example/CMakeLists.txt#L22

You can also try https://github.com/msgpack/msgpack-c/tree/c_master#install-from-git-repository instruction.

redboltz avatar Aug 25 '22 10:08 redboltz

Thank you. I have tried to reinstall the library with the []((https://github.com/msgpack/msgpack-c/tree/c_master#install-from-git-repository) instruction and the linking still fails. Then, I've tried by modifying the CMakeLists.txt file in the msgpack-c folder but I still get the linking error at the same line. Finally, I've tried by modifying the CMakeLists.txt file in the folder containing the "app_main.c" file and I the linker cannot find the "nvs_flash.h" file.

marco711 avatar Aug 25 '22 12:08 marco711

It seems that the issue is caused by your code not msgpack-c. I cant help you any more.

redboltz avatar Aug 25 '22 12:08 redboltz

OK, thanks for your time. I will recreate the project from scratch.

marco711 avatar Aug 25 '22 12:08 marco711

Hello, I have downloaded the MQTT-TCP example and I have downloaded and installed the msgpack-c library in the folder "tcp". Then, I have included the library (#include <msgpack.h>) and added some of its functions in the main function:

void app_main(void)
{
    ESP_LOGI(TAG, "[APP] Startup..");
    ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
    ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());

    esp_log_level_set("*", ESP_LOG_INFO);
    esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
    esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
    esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
    esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
    esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
    esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE);

    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
     * Read "Establishing Wi-Fi or Ethernet Connection" section in
     * examples/protocols/README.md for more information about this function.
     */
    ESP_ERROR_CHECK(example_connect());

    mqtt_app_start();
    
/* Messagepack code. */
	msgpack_sbuffer *buffer = msgpack_sbuffer_new();
	msgpack_packer *pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);

        for(int 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_bin(pk, 5);
           msgpack_pack_bin_body(pk, "Hello", 5);
           msgpack_pack_bin(pk, 11);
           msgpack_pack_bin_body(pk, "MessagePack", 11);
           msgpack_pack_int(pk, j);

           /* deserializes it. */
           msgpack_unpacked msg;
           msgpack_unpacked_init(&msg);
           msgpack_unpack_return ret = msgpack_unpack_next(&msg, buffer->data, buffer->size, NULL);

           /* prints the deserialized object. */
           msgpack_object obj = msg.data;
           msgpack_object_print(stdout, obj);  /*=> ["Hello", "MessagePack"] */
           msgpack_unpacked_destroy(&msg);
           puts("");
        }
        /* cleaning */
        msgpack_sbuffer_free(buffer);
        msgpack_packer_free(pk);	

}

The build fails with the same error (undefined reference to ...). Did I install the msgpack library in the wrong folder? Moreover, it is not clear for me how to modify the CMakeLists.txt files. Any help will be appreciated.

marco711 avatar Aug 26 '22 08:08 marco711

I have also build the project and tried to run the example programs, but getting the undefined reference error while linking..

vk015 avatar Apr 25 '23 12:04 vk015