Azure IoT SDK linux пример ошибки компиляции: в функции Send_Sample неопределенная ссылка на - PullRequest
0 голосов
/ 14 января 2020

Я знаю, что есть много вопросов, подобных этому, и я прочитал многие из них, но я не могу решить свою проблему.

Я пытаюсь скомпилировать пример кода azure iot SDK с g cc на Ubuntu.

Моя командная строка выглядит следующим образом:

xz@yz:~/workspace/.../samples/send$ gcc -I/usr/local/include/azure_c_shared_utility send.c main.c -o send

Я пытаюсь связать файлы заголовков с помощью команды -I. Я получил похожую ошибку, когда не связал файл main.c. Но это не работает. Когда я впервые попробовал команду -I, я получил другую ошибку (связанную с неправильным именованием), от которой я избавился, поэтому она должна работать в некоторой степени.

Заголовочные файлы находятся в папке / usr / local / include / и / usr / local / include / azure_c_shared_utility . Источник находится в ~ / рабочей области /.../ samples / send .

Это вывод моей ошибки.

    /tmp/cc854XPT.o: In function `Send_Sample':
send.c:(.text+0x31d): undefined reference to `xlogging_set_log_function'
send.c:(.text+0x354): undefined reference to `sprintf_s'
send.c:(.text+0x362): undefined reference to `EventHubClient_GetVersionString'
send.c:(.text+0x37b): undefined reference to `platform_init'
send.c:(.text+0x3b3): undefined reference to `EventHubClient_CreateFromConnectionString'
send.c:(.text+0x3fa): undefined reference to `EventHubClient_SetStateChangeCallback'
send.c:(.text+0x415): undefined reference to `EventHubClient_SetErrorCallback'
send.c:(.text+0x429): undefined reference to `EventHubClient_SetLogTrace'
send.c:(.text+0x442): undefined reference to `EventData_CreateWithNewMemory'
send.c:(.text+0x484): undefined reference to `EventData_SetPartitionKey'
send.c:(.text+0x4b3): undefined reference to `EventData_Properties'
send.c:(.text+0x4e1): undefined reference to `Map_Add'
send.c:(.text+0x50b): undefined reference to `EventHubClient_Send'
send.c:(.text+0x54d): undefined reference to `EventData_Destroy'
send.c:(.text+0x55c): undefined reference to `EventHubClient_Destroy'
send.c:(.text+0x561): undefined reference to `platform_deinit'
collect2: error: ld returned 1 exit status

Это пример кода:

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <stdio.h>
#include <stdarg.h>

#include "eventhubclient.h"
#include "eventdata.h"
#include "send.h"
#include "azure_macro_utils/macro_utils.h"
#include "azure_c_shared_utility/threadapi.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_c_shared_utility/platform.h"
#include "azure_c_shared_utility/xlogging.h"

static const char* connectionString = "Endpoint=sb://[namespace].servicebus.windows.net/;SharedAccessKeyName=[key name];SharedAccessKey=[key value]";
static const char* eventHubPath = "[event hub name]";

static bool g_bSendProperties = false;
static bool g_bSendPartitionKey = false;

static const char PARTITION_KEY_INFO[]  = "PartitionKeyInfo";
static const char TEST_STRING_VALUE_1[] = "Property_String_Value_1";
static const char TEST_STRING_VALUE_2[] = "Property_String_Value_2";

#define SLEEP_TIME      1000
#define BUFFER_SIZE     128
static unsigned int g_id = 1000;

MU_DEFINE_ENUM_STRINGS(EVENTHUBCLIENT_STATE, EVENTHUBCLIENT_STATE_VALUES);
MU_DEFINE_ENUM_STRINGS(EVENTHUBCLIENT_ERROR_RESULT, EVENTHUBCLIENT_ERROR_RESULT_VALUES);

void custom_logging_function(LOG_CATEGORY log_category, const char* file, const char* func, const int line, unsigned int options, const char* format, ...)
{
    va_list args;
    va_start(args, format);

    switch (log_category)
    {
        case AZ_LOG_INFO:
            (void)printf("Custom Info: ");
            break;
        case AZ_LOG_ERROR:
            (void)printf("Custom Error: File:%s Func:%s Line:%d ", file, func, line);
            break;
        default:
            break;
    }
    (void)vprintf(format, args);
    va_end(args);

    if (options & LOG_LINE)
    {
        (void)printf("\r\n");
    }
}

void eventhub_state_change_callback(EVENTHUBCLIENT_STATE eventhub_state, void* userContextCallback)
{
    (void)userContextCallback;
    (void)printf("eventhub_state_change_callback %s\r\n", MU_ENUM_TO_STRING(EVENTHUBCLIENT_STATE, eventhub_state) );
}

void eventhub_error_callback(EVENTHUBCLIENT_ERROR_RESULT eventhub_failure, void* userContextCallback)
{
    (void)userContextCallback;
    (void)printf("eventhub_error_callback %s\r\n", MU_ENUM_TO_STRING(EVENTHUBCLIENT_ERROR_RESULT, eventhub_failure) );
}

int Send_Sample(void)
{
    xlogging_set_log_function(custom_logging_function);

    int result;
    // Increment the id
    g_id++;

    char msgContent[BUFFER_SIZE];
    size_t msgLength = sprintf_s(msgContent, BUFFER_SIZE, "{\"messageId\":%u, \"name\":\"Send_Sample\"}", g_id);

    (void)printf("Starting the EventHub Client Send Sample (%s)...\r\n", EventHubClient_GetVersionString());

    if (platform_init() != 0)
    {
        (void)printf("ERROR: Failed initializing platform!\r\n");
        result = 1;
    }
    else
    {
        EVENTHUBCLIENT_HANDLE eventHubClientHandle = EventHubClient_CreateFromConnectionString(connectionString, eventHubPath);
        if (eventHubClientHandle == NULL)
        {
            (void)printf("ERROR: EventHubClient_CreateFromConnectionString returned NULL!\r\n");
            result = 1;
        }
        else
        {
            EventHubClient_SetStateChangeCallback(eventHubClientHandle, eventhub_state_change_callback, NULL);
            EventHubClient_SetErrorCallback(eventHubClientHandle, eventhub_error_callback, NULL);
            EventHubClient_SetLogTrace(eventHubClientHandle, true);

            EVENTDATA_HANDLE eventDataHandle = EventData_CreateWithNewMemory((const unsigned char*)msgContent, msgLength);
            if (eventDataHandle == NULL)
            {
                (void)printf("ERROR: eventDataHandle is NULL!\r\n");
                result = 1;
            }
            else
            {
                if (EventData_SetPartitionKey(eventDataHandle, PARTITION_KEY_INFO) != EVENTDATA_OK)
                {
                    (void)printf("ERROR: EventData_SetPartitionKey failed!\r\n");
                    result = 1;
                }
                else
                {
                    // Add the properties to the Event Data
                    MAP_HANDLE mapProperties = EventData_Properties(eventDataHandle);
                    if (mapProperties != NULL)
                    {
                        if (Map_Add(mapProperties, "SendHL_1", TEST_STRING_VALUE_1) != MAP_OK)
                        {
                            (void)printf("ERROR: Map_AddOrUpdate failed!\r\n");
                        }
                    }

                    if (EventHubClient_Send(eventHubClientHandle, eventDataHandle) != EVENTHUBCLIENT_OK)
                    {
                        (void)printf("ERROR: EventHubClient_Send failed!\r\n");
                        result = 1;
                    }
                    else
                    {
                        (void)printf("EventHubClient_Send.......Successful\r\n");
                        result = 0;
                    }
                }
                EventData_Destroy(eventDataHandle);
            }
            EventHubClient_Destroy(eventHubClientHandle);
        }

        platform_deinit();
    }

    (void)printf("Press any key to continue.");
    (void)getchar();
    return result; 
}

Большое спасибо за помощь!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...