c - 在 MakeFile 中使用调试器

标签 c debugging makefile

我已经写了一个完整的程序和它的 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/

相关文章:

c - 将 C 代码翻译成汇编

c - 为什么不同的格式说明符会使用 printf 给出不同的输出?

Eclipse 在使用 "Evaluating"线程调试期间挂起

java - Makefile:$(shell find ...) 未输出正确的结果

makefile - GNU 使 "Removing intermediate files"

linux - 是否可以链接 16 位代码和 32 位代码?

c - C 中相反方向的对角矩阵

c - printf() 正在打印错误的值

java - eclipse 断点 : stop before leaving a Java method

javascript - node.js 和段错误