生成文件。用头文件创建程序
Makefile. Create program with header file
我在通过 Makefile 编译我的程序时遇到问题。 Ofcorse 我读了很多有类似问题的主题,但我无法理解我的情况,因此我在编译时遇到问题。
这是我的程序,他是用 c 写的。简直是。这不是全部内容,但我想这足以理解 Makefile 中的问题。程序只有 3 个文件:
- main.c(包含 struct.h 并使用来自 struct.c 的对象)
- struct.h
- struct.c(包括struct.h)
main.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
您需要使 CommandStructure
变量在所有翻译单元中可见。为此,在头文件中将结构声明为 extern
。
此外,您必须从 struct.c
文件的 CommandStructure
中删除 static
存储-class-说明符。
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
此外,static
在 struct.c
中的用法恰恰相反 - 它确保符号 CommandStructure
仅在该翻译单元中可用。因此,您还应该从 struct.c
.
中删除 static
限定符
是的。就是这个。正确的是必须添加一个细节 [] or
#ifndef STRUCT
#define STRUCT
struct Command
{ char tableCmd[5];
char *NameCommand;
int size;
};
extern struct Command CommandStructure[]; // CommandStructure is table so should be []
#endif
我也把Makefile改成了这个
NAME=test
all:
gcc struct.c main.c -o $(HOME)/Pulpit/$(NAME)
并且编译所有内容都没有任何错误。所以,这不是 Makefile 的问题,而是语法 c 文件的问题。
谢谢
这里是建议的 makefile 内容:
CC := /bin/gcc
RM := /bin/rm
CFLAGS := -Wall -Wextra -pedantic -c -ggdb
LFLAGS :=
NAME := test
OBJS := main.o struct.o
.PHONY: all clean
all: $(NAME) $(OBJS)
%.o: %.c struct.h
<tab>$(CC) $(CFLAGS) $< -o $@ -I.
$(NAME): $(OBJS)
<tab>$(CC) $(LFLAGS) -o $@ $(OBJS)
clean:
<tab>$(RM) -f *.o
<tab>$(RM) -f %(NAME)
Note: use a tab char where I have used <tab>
关于 struct.c 文件
使用 'static' 使 CommandStructure[] 仅在该文件中可见。
建议删除 'static' 修饰符
关于 struct.h 文件
插入以下行以便main.c可以访问结构
extern struct Command CommandStruct[]
关于 main.c 文件
为 printf() 的正确原型插入以下行
#include <stdio.h>
建议学习 'for' 语句,因为它比 'while' 和 'i = i+1;' 语句更好
缩进代码时,始终使用空格,而不是制表符。
因为每个 editor/wordprocessor 都会根据个人喜好设置选项卡 stops/tab 宽度
为了便于阅读,建议在每个左大括号“{”后缩进 4 个空格
并在每个右大括号 '}'
之前取消缩进
我在通过 Makefile 编译我的程序时遇到问题。 Ofcorse 我读了很多有类似问题的主题,但我无法理解我的情况,因此我在编译时遇到问题。
这是我的程序,他是用 c 写的。简直是。这不是全部内容,但我想这足以理解 Makefile 中的问题。程序只有 3 个文件:
- main.c(包含 struct.h 并使用来自 struct.c 的对象)
- struct.h
- struct.c(包括struct.h)
main.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
您需要使 CommandStructure
变量在所有翻译单元中可见。为此,在头文件中将结构声明为 extern
。
此外,您必须从 struct.c
文件的 CommandStructure
中删除 static
存储-class-说明符。
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
此外,static
在 struct.c
中的用法恰恰相反 - 它确保符号 CommandStructure
仅在该翻译单元中可用。因此,您还应该从 struct.c
.
static
限定符
是的。就是这个。正确的是必须添加一个细节 [] or
#ifndef STRUCT
#define STRUCT
struct Command
{ char tableCmd[5];
char *NameCommand;
int size;
};
extern struct Command CommandStructure[]; // CommandStructure is table so should be []
#endif
我也把Makefile改成了这个
NAME=test
all:
gcc struct.c main.c -o $(HOME)/Pulpit/$(NAME)
并且编译所有内容都没有任何错误。所以,这不是 Makefile 的问题,而是语法 c 文件的问题。
谢谢
这里是建议的 makefile 内容:
CC := /bin/gcc
RM := /bin/rm
CFLAGS := -Wall -Wextra -pedantic -c -ggdb
LFLAGS :=
NAME := test
OBJS := main.o struct.o
.PHONY: all clean
all: $(NAME) $(OBJS)
%.o: %.c struct.h
<tab>$(CC) $(CFLAGS) $< -o $@ -I.
$(NAME): $(OBJS)
<tab>$(CC) $(LFLAGS) -o $@ $(OBJS)
clean:
<tab>$(RM) -f *.o
<tab>$(RM) -f %(NAME)
Note: use a tab char where I have used <tab>
关于 struct.c 文件
使用 'static' 使 CommandStructure[] 仅在该文件中可见。
建议删除 'static' 修饰符
关于 struct.h 文件
插入以下行以便main.c可以访问结构
extern struct Command CommandStruct[]
关于 main.c 文件
为 printf() 的正确原型插入以下行
#include <stdio.h>
建议学习 'for' 语句,因为它比 'while' 和 'i = i+1;' 语句更好
缩进代码时,始终使用空格,而不是制表符。 因为每个 editor/wordprocessor 都会根据个人喜好设置选项卡 stops/tab 宽度
为了便于阅读,建议在每个左大括号“{”后缩进 4 个空格 并在每个右大括号 '}'
之前取消缩进