我使用Makefile在C语言中编译我的代码。 但是,当我尝试使用就可以了Valgrind的,我不能看到泄漏的轨道:
这是我的Makefile:
SRC = main.c
OBJ = $(SRC:.c=.o)
NAME = valgrind_check
all: $(NAME)
$(NAME): $(OBJ)
gcc -g -o $(NAME) $(OBJ)
这是我启动valgrind的方法:
make
cc -c -o main.o main.c
gcc -g -o valgrind_check main.o
valgrind --track-origins=yes --leak-check=full ./valgrind_check
现在的结果是:
==13934== Memcheck, a memory error detector
==13934== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13934== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13934== Command: ./valgrind_check
==13934==
==13934== Conditional jump or move depends on uninitialised value(s)
==13934== at 0x4E8B4A9: vfprintf (in /usr/lib64/libc-2.27.so)
==13934== by 0x4E935F5: printf (in /usr/lib64/libc-2.27.so)
==13934== by 0x400501: bad_function (in /home/mrabaud/Repository/Valgrind/valgrind_check)
==13934== by 0x400524: main (in /home/mrabaud/Repository/Valgrind/valgrind_check)
==13934== Uninitialised value was created by a stack allocation
==13934== at 0x4004E6: bad_function (in /home/mrabaud/Repository/Valgrind/valgrind_check)
==13934==
==13934== Use of uninitialised value of size 8
==13934== at 0x4E8792E: _itoa_word (in /usr/lib64/libc-2.27.so)
==13934== by 0x4E8B225: vfprintf (in /usr/lib64/libc-2.27.so)
==13934== by 0x4E935F5: printf (in /usr/lib64/libc-2.27.so)
==13934== by 0x400501: bad_function (in /home/mrabaud/Repository/Valgrind/valgrind_check)
==13934== by 0x400524: main (in /home/mrabaud/Repository/Valgrind/valgrind_check)
==13934== Uninitialised value was created by a stack allocation
==13934== at 0x4004E6: bad_function (in /home/mrabaud/Repository/Valgrind/valgrind_check)
==13934==
如您所见,跟踪根本没有帮助。
下面的结果,如果我运行该代码而不生成文件:
gcc -g -o valgrind_check main.c
valgrind --track-origins=yes --leak-check=full ./valgrind_check
==14056== Memcheck, a memory error detector
==14056== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==14056== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==14056== Command: ./valgrind_check
==14056==
==14056== Conditional jump or move depends on uninitialised value(s)
==14056== at 0x4E8B4A9: vfprintf (in /usr/lib64/libc-2.27.so)
==14056== by 0x4E935F5: printf (in /usr/lib64/libc-2.27.so)
==14056== by 0x400501: bad_function (main.c:14)
==14056== by 0x400524: main (main.c:22)
==14056== Uninitialised value was created by a stack allocation
==14056== at 0x4004E6: bad_function (main.c:11)
==14056==
==14056== Use of uninitialised value of size 8
==14056== at 0x4E8792E: _itoa_word (in /usr/lib64/libc-2.27.so)
==14056== by 0x4E8B225: vfprintf (in /usr/lib64/libc-2.27.so)
==14056== by 0x4E935F5: printf (in /usr/lib64/libc-2.27.so)
==14056== by 0x400501: bad_function (main.c:14)
==14056== by 0x400524: main (main.c:22)
==14056== Uninitialised value was created by a stack allocation
==14056== at 0x4004E6: bad_function (main.c:11)
我该如何解决?我没有选择,我必须使用一个Makefile。
任何帮助将不胜感激。我这个月有这个问题,但一无所获......
答案 0 :(得分:1)
您没有包含有关如何构建.o文件的规则,因此使用了默认规则。该默认规则未使用-g标志编译main.c,因此您将无法获得调试信息。
添加一个规则的.o拼写了这一点:
%.o: %.c
gcc -g -c $<