简体   繁体   中英

What functions are used from an imported header in c?

I am reading through this huge code base which is written in C. In some of the files there are some headers which are included, the need for which is not specified. I wanted to know if there is any way that I can look at what functions are used from a particular header in the current file without having to read through the whole code in both files.

The simplest way to do so is checking the preprocessed file. The compiler's preprocessor will generate information which file the declaration or definition comes from.

use GCC or Clang, with flags like -E to generate the preprocessed code.

Example

Take the modified version of helloworld as an example. The code is modified to include more header files.

#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;
}

In my Ubuntu 15.04, use gcc -E hello_mod.c -o hello_mod.i . The preprocessed file is as following, Unrelated code have been removed.

<......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;
}

Usually, the original code is at the end of the preprocessor generated file. Then you could search backward for the function you are interested to. Once you find it. Then the first file path started with # when you scrolling backward the file is the header file which the function declares.

Based on the above code, we could find out that:

  • int16_t is typedef ed in file /usr/include/stdint.h .
  • printf function is declared in /usr/include/stdio.h .
  • malloc and free function are declared in /usr/include/stdlib.h

Compiler also use this way to generate the debug information which helps you to find the file path and line number in gdb.

There are a few options:

1. Comment out the header, see what breaks

One quick way to see whether something is needed is to comment out a header file and see what breaks on compilation. There are some pitfalls with this, though, because you might still be including that header in some other file.

You can use the -M flag with GCC to get a list of dependencies, like this:

    gcc -M file.c

Or, if you want to exclude all the system includes you can use:

    gcc -MM file.c

This will give you a list of what headers include what other headers. This doesn't directly tell you which functions came from where, but it will help you figure out all the places a given header might be working its way into the code.

2. Grab preprocessor output.

Stop compilation of your file by using

   gcc -E file.c > file.txt

This will give you the output that the preprocessor sends to the compiler. You'll need to redirect the output, as this option places its output on stdout.

3. Take a look at the man pages

Most standard headers have man pages. You can do, for example

    man stdio

to see a page for what is included in stdio.h If you've never looked through these, they are often surpisingly educational.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM