小编lin*_*ell的帖子

你如何构造一个 C makefile 以便它编译 source.c 文件?

我有三个文件。main.c、graph.c 和 graph.h。我知道代码在没有 makefile 的情况下也能工作,因为我在 onlinegbd 编译器上对其进行了测试。一切顺利。

当我尝试在终端中运行我的 makefile 时,出现以下错误:

undefined reference to "function"
Run Code Online (Sandbox Code Playgroud)

其中“函数”是我在 main 中调用的每个函数,该函数位于 graph.c 中。

所以这让我认为我没有在我的 makefile 中编译 graph.c。

编辑 我已经确认它是生成文件。我编译它使用:

gcc -o xGraph main.c graph.c
Run Code Online (Sandbox Code Playgroud)

它运行没有问题。这是生成文件:

CC = gcc

VPATH = SRC INCLUDE

TARGET = XGraph

CFLAGS = -g -Wall

all: $(TARGET)

$(TARGET): main.o graph.o
    $(CC) main.o graph.o -o $(TARGET)

main.o: main.c graph.h
    $(CC) $(CFLAGS) main.c

graph.o: graph.c graph.h
    $(CC) $(CFLAGS) graph.c

clean:
    rm *.o *~ $(TARGET)
Run Code Online (Sandbox Code Playgroud)

c makefile

2
推荐指数
1
解决办法
64
查看次数

标签 统计

c ×1

makefile ×1