make: *** [main.o] 错误 1

sou*_*uha 2 gcc makefile

我正在执行一个简单的 makefile,其中包含 3 个部分,但它不能很好地工作,这些是我的文件 .h 和 .c 的详细信息:

  1. 主程序

    #include <stdio.h>
    #include <stdlib.h>
    #include "hello.h"
    
    int main (void)
    {
         hello();
         return EXIT_SUCCESS;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 你好.h

    #ifndef hello
        #define hello
        void hello (void);
    #endif
    
    Run Code Online (Sandbox Code Playgroud)
  3. 你好ç

    #include <stdio.h>
    #include <stdlib.h>
    
    void hello (void)
    {
        printf("Hello World\n");
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 生成文件

    all: hello
    hello: hello.o main.o
         gcc -o hello hello.o main.o
    
    hello.o: hello.c
         gcc -o hello.o -c hello.c -W -Wall -ansi -pedantic
    
    main.o: main.c hello.h
        gcc -o main.o -c main.c -W -Wall -ansi -pedantic
    
    clean:
         rm -rf *.o
    
     mrproper: clean
         rm -rf hello
    
    Run Code Online (Sandbox Code Playgroud)

我收到此错误:

图像

Iva*_*nov 5

当您编写时#define hello,您定义hello为一个空令牌。因此,下一个字符串的函数声明实际上变成了这样:

void (void);
Run Code Online (Sandbox Code Playgroud)

这不是有效的 C 代码。

您想要做的可能是Include Guard,其目的是避免多次包含一个标头。守卫的名称必须与您使用的任何其他令牌不同。通常的命名是FILENAME_H

#ifndef HELLO_H
#define HELLO_H
void hello(void);
#endif
Run Code Online (Sandbox Code Playgroud)