• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

Make&&Valgrind

2020-01-26

1. Make

2. 隐式声明

1
2
3
4
5
6
➜  dumpc make ex1
cc ex1.c -o ex1
ex1.c: In function ‘main’:
ex1.c:4:5: warning: implicit declaration of function ‘puts’ [-Wimplicit-function-declaration]
puts("Hello world.");
^~~~

3. Makefile

Make会自动假设当前文件夹中有一个叫做Makefile的文件,并且会执行它。此外,一定要注意:确保你只输入了 TAB 字符,而不是空格和 TAB 的混合

  • 首先我们在文件中设置CFLAGS,所以之后就不用再设置了。并且,我们添加了-g标识来获取调试信息

    • CFLAGS="-Wall"会给make通常使用的cc命令添加-Wall选项。这行命令告诉cc编译器要报告所有的警告(然而实际上不可能报告所有警告)
  • 接着我们写了一个叫做clean的部分,它告诉make如何清理我们的小项目

1
2
3
4
CFLAGS=-Wall -g

clean:
    rm -f ex1

如果出现Makefile:4: *** missing separator. Stop.则说明Makefile格式不对

4. Valgrind

简介

  • Valgrind (/ˈvælɡrɪnd/) is a programming tool for memory debugging, memory leak detection, and profiling.

  • Valgrind was originally designed to be a free memory debugging tool for Linux on x86, but has since evolved to become a generic framework for creating dynamic analysis tools such as checkers and profilers

安装

  • sudo apt install valgrind

用法

  • valgrind ./ex4

  • 示例:printf("I am %d years old.\n");

结果

  •  错误信息

    1
    2
    3
    4
    5
    6
    7
    ==3876== Memcheck, a memory error detector
    ==3876== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==3876== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
    ==3876== Command: ./ex4
    ==3876==
    ==3876== error calling PR_SET_PTRACER, vgdb might block
    I am -16776728 years old.
  • if语句或者while循环基于一个未初始化的值

    1
    2
    3
    4
    5
    ==3876== Conditional jump or move depends on uninitialised value(s)
    ==3876== at 0x4E988DA: vfprintf (vfprintf.c:1642)
    ==3876== by 0x4EA0F25: printf (printf.c:33)
    ==3876== by 0x10867F: main (ex4.c:11)
    ==3876==
  • 有“大小为8的未初始化的值”,并且在它下面看到了“栈踪迹”

    1
    2
    3
    4
    5
    ==3876== Conditional jump or move depends on uninitialised value(s)
    ==3876== at 0x4E988DA: vfprintf (vfprintf.c:1642)
    ==3876== by 0x4EA0F25: printf (printf.c:33)
    ==3876== by 0x10867F: main (ex4.c:11)
    ==3876==
  • 摘要

    1
    2
    3
    4
    5
    6
    7
    8
    9
    ==3876== HEAP SUMMARY:
    ==3876== in use at exit: 0 bytes in 0 blocks
    ==3876== total heap usage: 1 allocs, 1 frees, 4,096 bytes allocated
    ==3876==
    ==3876== All heap blocks were freed -- no leaks are possible
    ==3876==
    ==3876== For counts of detected and suppressed errors, rerun with: -v
    ==3876== Use --track-origins=yes to see where uninitialised values come from
    ==3876== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)
  • Program Language
  • C
  • Basic
git笔记
Python入门-2
  1. 1. 1. Make
    1. 1.1. 2. 隐式声明
    2. 1.2. 3. Makefile
  2. 2. 4. Valgrind
© 2024 何决云 载入天数...