makefile失败 - 隐式规则编译一个目标文件但不编译其余文件

问题描述 投票:1回答:1

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

这是我的makefile:

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.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>
makefile
1个回答
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中的缩进,而不是空格。

© www.soinside.com 2019 - 2024. All rights reserved.