Skip to content

Cannot link project using msgpack-c library #1031

Open
@marco711

Description

@marco711

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?

Activity

redboltz

redboltz commented on Aug 24, 2022

@redboltz
Contributor

msgpack_pack_str_body is an inline function.

msgpack_pack_inline_func(_str_body)(msgpack_pack_user x, const void* b, size_t l)
{
msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
}

msgpack_zone_free is not an inline function.

msgpack-c/src/zone.c

Lines 217 to 222 in c3df1bb

void msgpack_zone_free(msgpack_zone* zone)
{
if(zone == NULL) { return; }
msgpack_zone_destroy(zone);
free(zone);
}

So you need to link the library libmsgpackc.a.

marco711

marco711 commented on Aug 24, 2022

@marco711
Author

Thank you. How do I link this library?

redboltz

redboltz commented on Aug 24, 2022

@redboltz
Contributor

See your compiler/linker manual. Usualy -lmsgpackc.

marco711

marco711 commented on Aug 25, 2022

@marco711
Author

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.

redboltz

redboltz commented on Aug 25, 2022

@redboltz
Contributor

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.

marco711

marco711 commented on Aug 25, 2022

@marco711
Author

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.

redboltz

redboltz commented on Aug 25, 2022

@redboltz
Contributor

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

marco711

marco711 commented on Aug 25, 2022

@marco711
Author

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

marco711

marco711 commented on Aug 26, 2022

@marco711
Author

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.

vk015

vk015 commented on Apr 25, 2023

@vk015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @redboltz@marco711@vk015

        Issue actions

          Cannot link project using msgpack-c library · Issue #1031 · msgpack/msgpack-c