gpt4 book ai didi

c - 生成文件。用头文件创建程序

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:42 25 4
gpt4 key购买 nike

我在通过 Makefile 编译我的程序时遇到问题。当然,我读过很多有类似问题的主题,但我无法理解我的情况,因此我在编译时遇到了问题。

这是我的程序,他是用 c 语言编写的。简直是。这不是全部内容,但我想这足以理解 Makefile 中的问题。程序只有 3 个文件:

  • main.c(包含 struct.h 并使用 struct.c 中的对象)
  • 结构.h
  • struct.c(包含struct.h)

主.c

#include "struct.h"
#define SIZE_STRUCT 2
int main()
{
int i = 0;
while(i < 2)
{

printf("Contens %d /n" , CommandStructure[i].size)
i = i +1;
}

return 0;
}

struct.h

#ifndef STRUCT
#define STRUCT


struct Command
{ char tableCmd[5];
char *NameCommand;
int size;

};

#endif

struct.c

#include "struct.h"

static struct Command CommandStructure[]={
{
.tableCmd = {0x3,0x5,0x4,0x4,0x5},
.NameCommand = "SOMEWHERE",
.size = 11,
},{
.tableCmd = {0x6, 0x34, 0x40, 0x22, 0x4},
.NameCommand = "SOMETHING",
.size = 12,
}
};

还有我的主要问题 Makefile

NAME=test

all: main.c struct.c struct.h
gcc struct.c main.c -o $(HOME)/Pulpit/$(NAME)

当然我得到错误

error: ‘CommandStructure’ undeclared (first use in this function) if(!strncmp(buf , CommandStructure[i].NameCommand , CommandStructure[i].size)) main.c:140:27: note: each undeclared identifier is reported only once for each function it appears in Makefile:6: polecenia dla obiektu 'all' nie powiodły się make: *** [all] Error1

最佳答案

struct.c 中的声明对 main.c 不可见。您需要在 struct.h 中声明 CommandStructure,如下所示:

#ifndef STRUCT
#define STRUCT


struct Command
{ char tableCmd[5];
char *NameCommand;
int size;

};

extern struct Command CommandStructure[];

#endif

此外,struct.cstatic 的使用恰恰相反 - 它确保符号 CommandStructure 仅在那个翻译单位。因此,您还应该从 struct.c 中删除 static 限定符。

关于c - 生成文件。用头文件创建程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31205430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com