【问题标题】:Makefile. Create program with header file生成文件。用头文件创建程序
【发布时间】:2015-09-21 05:08:21
【问题描述】:

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

这是我在 c 上编写的程序。简直就是。这不是全部内容,但我认为这足以理解 Makefile 中的问题。程序只有 3 个文件:

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

ma​​in.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)

当然我得到错误

错误:“CommandStructure”未声明(在此函数中首次使用) if(!strncmp(buf, CommandStructure[i].NameCommand, CommandStructure[i].size)) main.c:140:27: 注意:每个未声明的标识符只报告一次 它出现在 Makefile:6 中的函数:polecenia dla obiektu 'all' nie powiodły się make: *** [all] Error1

【问题讨论】:

  • 你永远不会在 main.c 中定义 CommandStructure
  • @bmargulies its' 在 struct.c 文件中,不是吗?
  • 致@OP,你知道你错过了stdio.h吗?

标签: c struct makefile header-files


【解决方案1】:

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

此外,staticstruct.c 中的使用正好相反——它确保符号CommandStructure 仅在该翻译单元中可用。因此,您还应该从struct.c 中删除static 限定符。

【讨论】:

    【解决方案2】:

    您需要使 CommandStructure 变量在所有翻译单元中可见。为此,在头文件中将结构声明为extern

    此外,您必须从 CommandStructurein struct.c 文件中删除 static storage-class-specifier。

    【讨论】:

      【解决方案3】:

      这是一个建议的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 文件

      使用“静态”使 CommandStructure[] 仅在该文件中可见。

      建议去掉'static'修饰符

      关于 struct.h 文件

      插入以下行,以便 main.c 可以访问该结构

      extern struct Command CommandStruct[]
      

      关于 main.c 文件

      插入以下行以获得正确的 printf() 原型

      #include <stdio.h>
      

      建议学习 'for' 语句,因为它比 'while' 和 'i = i+1;' 更好陈述

      缩进代码时,请始终使用空格,而不是制表符。 因为每个编辑器/文字处理器都会根据个人喜好设置制表位/制表位宽度

      为了便于阅读,建议在每个左大括号 '{' 后缩进 4 个空格 并且在每个右大括号 '}' 之前取消缩进

      【讨论】:

        【解决方案4】:

        是的。就是这个。正确必须添加一个细节 []

        #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)
        

        并且 evrythings 编译时没有任何错误。所以,这不是 Makefile 的问题,而是语法 c 文件的问题。

        谢谢

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-20
          • 1970-01-01
          • 2011-04-14
          • 1970-01-01
          • 2021-11-04
          • 1970-01-01
          • 2019-02-24
          相关资源
          最近更新 更多