我已经写了一个完整的程序和它的 makefile,唯一的麻烦是我必须知道如何实现调试器。我一直在网上查看类似的问题,但无济于事。
这是我的生成文件:
# the variable CC is the compiler to use.
CC=gcc
# the variable CFLAGS is compiler options.
CFLAGS=-c -Wall
assign4: main1.o ObjectManager.o
$(CC) main1.o ObjectManager.o -o assign4
main1.o: main1.c ObjectManager.h
$(CC) $(CFLAGS) main1.c
ObjectManager.o: ObjectManager.c ObjectManager.h
$(CC) $(CFLAGS) ObjectManager.c
clean:
rm -rf *o assign4
我已经尝试将 CFLAGS: 调整为 -g Wall,但它只是说:
Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [ObjectManager.o] Error 1
有什么解决这个问题的建议吗?
最佳答案
看这条规则:
ObjectManager.o: ...
$(CC) $(CFLAGS) ObjectManager.c
如果CFLAGS
是-c -Wall
,那么-c 意味着gcc 将编译源文件,但不会尝试链接它。 这正是我们想要的,生成目标文件而不是可执行文件。这样做的一个结果是源文件 (ObjectManager.c
) 不需要包含 main
函数——编译器信任您在链接时提供一个函数。
但如果 CFLAGS
是 -g -Wall
,则 -g 不暗示 -c。因此 gcc 将尝试创建一个可执行文件,如果 main
不存在则中止。
解决方法:
CFLAGS := -Wall -g # the -g can be added with a conditional if you like
ObjectManager.o: ...
$(CC) $(CFLAGS) -c ObjectManager.c
关于c - 在 MakeFile 中使用调试器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966820/