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

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

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

undefined reference to "function"

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

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

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

gcc -o xGraph main.c graph.c

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

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)

回答

编译 C 代码时,第一阶段是生成.o文件。将-c标志传递给 gcc 以执行此操作。


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

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


以上是你如何构造一个Cmakefile以便它编译source.c文件?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>