如何在Makefile中创建对.o文件的引用 [英] How to create reference to an .o file in Makefile

查看:619
本文介绍了如何在Makefile中创建对.o文件的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我提供了两个.o文件和相应的.h文件用于分配,但是我不知道如何使编译器使用.o文件.这是我当前正在使用的Makefile:

I was given two .o files with corresponding .h files to use for an assignment, but I do not know how to get the compiler to use the .o files. This is the Makefile I am currently using:

    TARGET = prog
LIBS = -lm
CC = gcc
CFLAGS = -g -Wall

.PHONY: default all clean

default: $(TARGET)
all: default

OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)

%.o: %.c $(HEADERS)
    $(CC) $(CFLAGS) -c $< -o $@

.PRECIOUS: $(TARGET) $(OBJECTS)

$(TARGET): $(OBJECTS)
    $(CC) $(OBJECTS) -Wall $(LIBS) -o $@

clean:
    -rm -f *.o
    -rm -f $(TARGET)

我认为我需要在文件末尾添加file1.o和file2.o,但是我不确定这是否正确.适当时,我确实在C源文件中包含.h文件,因此,我想到的编译错误的唯一原因是.o文件未使用我的代码进行编译.

I believe I need to add the file1.o and file2.o at the end, but I am not sure if that is right. I do have the .h files in the C source files when appropriate, so the only reason that I can think of for the compilation error is that the .o files are not being compiled with my code.

推荐答案

为提供的.o(例如)添加定义:

Add a define for the provided .o's (e.g.):

PREBUILT_O = fludger.o ramble.o plexor.o

将目标规则更改为:

$(TARGET): $(OBJECTS) $(PREBUILT_O)
    $(CC) $(OBJECTS) $(PREBUILT_O) $(LIBS) -o $@

这篇关于如何在Makefile中创建对.o文件的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆