【问题标题】:makefile fails - implicit rule compiles one object file but doesn't compile the restmakefile 失败 - 隐式规则编译一个目标文件,但不编译其余文件
【发布时间】:2018-09-13 00:31:58
【问题描述】:

Makefile 未正确使用隐式规则。我正在关注本指南here

这是我的生成文件:

objects = main.o hello.o
hello : $(objects)
    cc -o hello $(objects)

hello.o : defs.h
main.o : defs.h hello.h

.PHONY : clean
clean :
    -rm hello $(objects)

我收到以下错误:

cc: error: main.o: No such file or directory

它创建目标代码 hello.o,但不为 main.c 创建。如果我交换行并且 main 在上面,它将创建 main.o 但不会创建 hello.o。

这是我的 main.c 文件:

#include "defs.h"
#include "hello.h"

int main(int argc, char** argv) {
   display_hello();
   return (EXIT_SUCCESS);
}

这是我的 hello.c 文件:

#include "defs.h"

void display_hello()
{
    printf("Hello!\n");
}

我的 hello.h 文件:

#ifndef HELLO_H
#define HELLO_H

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif /* HELLO_H */

void display_hello();

这是我的 defs.h 文件:

#ifndef DEFS_H
#define DEFS_H

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif /* DEFS_H */

#include <stdio.h>
#include <stdlib.h>

【问题讨论】:

  • 您使用的是哪个版本的 Make? (如果不确定,请尝试make -v。)
  • 这是 GNU Make 4.1。现在试用 Autotools,它似乎更容易使用。

标签: makefile


【解决方案1】:

对我来说很好,我将文件创建为https://gist.github.com/boyvinall/f23420215707fa3e73e21c3f9a5ff22b

$ make
cc    -c -o main.o main.c
cc    -c -o hello.o hello.c
cc -o hello main.o hello.o

可能是 @Beta 所说的 make 的版本,但即使是旧版本的 GNU make 也应该可以正常工作。

否则,请确保您在 makefile 中使用制表符来缩进,而不是空格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    相关资源
    最近更新 更多