gcc - 使 : *** [main. o] 错误 1

标签 gcc makefile

我正在执行一个简单的 makefile,它包含 3 个部分,但效果不佳这些是我的文件 .h 和 .c 的详细信息:

  1. 主.c

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

    #ifndef hello
        #define hello
        void hello (void);
    #endif
    
  3. 你好.c

    #include <stdio.h>
    #include <stdlib.h>
    
    void hello (void)
    {
        printf("Hello World\n");
    }
    
  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
    

我收到这个错误:

Image

最佳答案

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

void (void);

这不是有效的 C 代码。

您尝试做的可能是 Include guard ,其目的是避免一个 header 被多次包含。 guard 的名字必须与您使用的任何其他 token 不同。通常的命名是 FILENAME_H:

#ifndef HELLO_H
#define HELLO_H
void hello(void);
#endif

关于gcc - 使 : *** [main. o] 错误 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40051500/

相关文章:

makefile - 如何理解这个 Go makefile?

c - 什么是 C 的高效工作流程? - 生成文件 + bash 脚本

c - 编译时,我写 "gcc -g -Wall dene2 dene2.c",然后 gcc 发出一些痕迹

mysql - 每次用 mysql.h 编译时,我都需要笨拙的 gcc 选项吗?

c++ - 在 gcc 中使用结构作为 SSE vector 类型?

c - Makefile 与 obj 文件不匹配

c - 编译和运行任何单个 C 文件的最快方法是什么

c - 如何在 BSD make 中强制报错

c++ - "pragma GCC diagnostic ignored"如果在 PCH 中定义就没有作用?

c - 海湾合作委员会 7.2 : warning: left shift count >= width of type