ESP-IDF 中的“多重定义”错误

问题描述 投票:0回答:1

当我编译 esp32 程序时,出现多重定义错误。有人能帮我吗? 这是我的 main.c 文件。

#include <stdio.h>
#include "test.h"
#include "esp_log.h"

static const char *TAG = "Omni-wheel";

void app_main(void)
{
    while(1){
        something();
        ESP_LOGI(TAG, "%f \n", sensorData.ax);
    }
}

这是我的 test.h 文件。

#ifndef _TEST_H_
#define _TEST_H_

struct SomeData
{
    float x;
} someData;

void something(void);

#endif

最后这是我的 test.c 文件。

#include "test.h"

void something(void){
    sensorData.ax = 35;
}

我寻找解决方案,但仍然无法解决该错误。 这是错误信息。

/home/knight/.espressif/tools/riscv32-esp-elf/esp-12.2.0_20230208/riscv32-esp-elf/bin/../lib/gcc/riscv32-esp-elf/12.2.0/../../../../riscv32-esp-elf/bin/ld: esp-idf/main/libmain.a(test.c.obj):/home/knight/Omni-wheel/main/test.h:7: multiple definition of `someData'; esp-idf/main/libmain.a(main.c.obj):/home/knight/Omni-wheel/main/test.h:7: first defined here
c compiler-errors esp32
1个回答
0
投票

您的头文件包含名为

struct SomeData
someData
类型变量的定义。由于此标头包含在多个源文件中,因此每个源文件都定义此变量。然后,当链接源文件时,您会收到多重定义错误。

您想在头文件中声明变量:

#ifndef _TEST_H_
#define _TEST_H_

struct SomeData
{
    float x;
};

extern struct SomeData someData;

void something(void);

#endif

并且定义它在一个.c文件中:

#include "test.h"

struct SomeData someData;

void something(void){
    sensorData.ax = 35;
}
© www.soinside.com 2019 - 2024. All rights reserved.