我使用 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==
如您所见,跟踪根本没有帮助。
这里是我在没有 makefile 的情况下运行代码的结果:
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。
任何帮助将不胜感激。我这个月有这个问题,但什么也没发现......
最佳答案
您没有包含有关如何构建 .o 文件的规则,因此使用了默认规则。该默认规则未使用 -g 标志编译 main.c,因此您无法获得调试信息。
为 .o 添加规则来拼写此内容:
%.o: %.c
gcc -g -c $<
关于C - Valgrind 不显示 Makefile 编译的代码的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467043/