error: dm_mmc_ops undeclared (first use in this function)

error: dm_mmc_ops undeclared (first use in this function)

我正在使用 Buildroot 为我的嵌入式系统 (cyclone V) 构建一个 u-boot 引导加载程序,但出现以下错误:

error: 'dm_mmc_ops' undeclared (first use in this function)

在多次尝试 understand/solve 错误之后,我设法隔离了看起来像下面的简单代码的问题,并产生了相同的错误:

File1.h

#ifndef FILE1
#define FILE1

struct dm_mmc_ops {
    int (*send_cmd)(int data);
    int (*set_ios)(char* dev);
};

struct dev {
    struct dm_mmc_ops* ops;
} *dev;

#define mmc_get_ops(dev)        ((dm_mmc_ops *)(dev)->ops)

#endif

File2.h

#ifndef FILE2
#define FILE2

#include "file1.h"

extern const struct dm_mmc_ops dm_dwmci_ops;

#endif

File2.c

#include <stdio.h>
#include "file1.h"
#include "file2.h"

int return_int (int data)
{
    return data;
}

int return_ptr (char* data)
{
    return (int) data;
}


const struct dm_mmc_ops dm_dwmci_ops = {
    .send_cmd   = return_int,
    .set_ios    = return_ptr
};

void main (void)
{
    struct dev my_dev = {.ops = &dm_dwmci_ops};
    dev = &my_dev;
    char text[] = "abcd";


    struct dm_mmc_ops *test_mmc = mmc_get_ops(dev);  // Error is here !!!

    printf("%d\n",test_mmc->send_cmd(50));
    printf("%d\n",text);
    printf("%d\n",test_mmc->set_ios(text));
    return;
}

那么产生的错误是:

error: 'dm_mmc_ops' undeclared (first use in this function)

我的代码有什么问题,我应该怎么做才能消除这个错误?

你的问题就在这里

#define mmc_get_ops(dev)        ((dm_mmc_ops *)(dev)->ops)
                                  ^^^^^^^^^^

你可能想要

#define mmc_get_ops(dev)        ((struct dm_mmc_ops *)(dev)->ops)

除此之外,您还有许多其他问题。将编译器设置为高警告级别(例如 gcc -Wall ...),然后修复所有警告。