What functions are used from the imported header in c?

I read this huge code base, which is written in C. Some files have some headers that are included, the need for which is not indicated. I wanted to know if there is any way by which I can see which functions are used from a specific header in the current file, without having to read all the code in both files.

+4
source share
2 answers

The easiest way to do this is to check the pre-processed file. The compiler preprocessor will generate information in which the declaration or definition is written.

use GCC or Clang, with flags such as -Eto generate pre-processed code.

Example

helloworld. .

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
        char * str = (char*) malloc(sizeof(char)*10);
        int16_t a = 10;
        str[0]='h';str[1]='e';str[2]='l';str[3]='l';str[4]='o';str[5]='\0';
        printf("%s %d\n", str, a);
        free(str);
        return 0;
}

Ubuntu 15.04 gcc -E hello_mod.c -o hello_mod.i. : Unrelated code .

<......UNRELATED CODE REMOVED.......>

# 36 "/usr/include/stdint.h" 3 4
typedef signed char int8_t;
typedef short int int16_t;
<......UNRELATED CODE REMOVED.......>


# 319 "/usr/include/stdio.h" 3 4
<......UNRELATED CODE REMOVED.......>

extern int printf (const char *__restrict __format, ...);

<......UNRELATED CODE REMOVED.......>


# 315 "/usr/include/stdlib.h" 2 3 4

<......UNRELATED CODE REMOVED.......>

extern void *malloc (size_t __size) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ;

<......UNRELATED CODE REMOVED.......>

extern void free (void *__ptr) __attribute__ ((__nothrow__ , __leaf__));


<......UNRELATED CODE REMOVED.......>
# 1 "/usr/include/x86_64-linux-gnu/bits/stdlib-float.h" 1 3 4
# 955 "/usr/include/stdlib.h" 2 3 4
# 967 "/usr/include/stdlib.h" 3 4

# 4 "hello_mod.c" 2

int main() {
        char * str = (char*) malloc(sizeof(char)*10);
        int16_t a = 10;
        str[0]='h';str[1]='e';str[2]='l';str[3]='l';str[4]='o';str[5]='\0';
        printf("%s %d\n", str, a);
        free(str);
        return 0;
}

. , . . # - , .

, :

  • int16_t typedef ed /usr/include/stdint.h.
  • printf ​​ /usr/include/stdio.h.
  • malloc free /usr/include/stdlib.h

, gdb.

+2

:

1. , ,

, -, , . , .

-M GCC , :

    gcc -M file.c

, , :

    gcc -MM file.c

, . , , , .

2. .

   gcc -E file.c > file.txt

, . , stdout.

3.

. ,

    man stdio

, stdio.h , .

+2

All Articles