如何在 makefile 上进行语法控制和调试?我使用了 g++ 编译器。我们可以假设以下代码是我们的示例 makefile。感谢您的建议。
all: sample1
sample1: deneme.o hello.o
g++ deneme.o hello.o -o sample1
deneme.o: deneme.cpp
g++ -c deneme.cpp
hello.o : hello.cpp
g++ -c hello.cpp
如何在 makefile 上进行语法控制和调试?我使用了 g++ 编译器。我们可以假设以下代码是我们的示例 makefile。感谢您的建议。
all: sample1
sample1: deneme.o hello.o
g++ deneme.o hello.o -o sample1
deneme.o: deneme.cpp
g++ -c deneme.cpp
hello.o : hello.cpp
g++ -c hello.cpp
通常,您有 makefile 变量,例如:
DEBUG=-Wall -g
并在您的构建命令中使用它们:
sample1: deneme.o hello.o
g++ deneme.o hello.o -o sample1
sample1-debug: deneme-debug hello-debug
g++ $(DEBUG) deneme.o hello.o -o sample1
deneme.o: deneme.cpp
g++ -c deneme.cpp
deneme-debug: deneme.cpp
g++ $(DEBUG) -c deneme.cpp
hello.o: hello.cpp
g++ -c hello.cpp
hello-debug: hello.cpp
g++ $(DEBUG) -c hello.cpp
然后make sample1-degug
为您调试可执行文件。
至于个人经验,我会保留makefile的原始结构,并在每个g++配方行中插入CDEBUG变量。(当然,makefile 可以用static-patterns来改进,这里不是这样)。这样,生成可调试程序所需要做的就是更改 Makefile 中的 CDEBUG 声明或在 make 调用中覆盖“make "CDEBUG=-g"”。
CDEBUG := -g #(or -ggdb, -g1- -g2 -gdwarf and so on)
all: sample1
sample1: deneme.o hello.o
g++ deneme.o hello.o -o sample1
deneme.o: deneme.cpp
g++ $(CDEBUG) -c deneme.cpp
hello.o : hello.cpp
g++ $(CDEBUG) -c hello.cpp
Paul 提供的解决方案会起作用,但请注意,它会创建大量 *-debug 文件,这些文件并不重要。但是,我当然很乐意以其他方式理解。