最简单的方法是检查预处理文件。编译器的预处理器将生成声明或定义来自哪个文件的信息。
使用 GCC 或 Clang,带有类似标志-E
生成预处理代码。
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 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
is typedef
编辑到文件中/usr/include/stdint.h
.
-
printf
函数声明于/usr/include/stdio.h
.
-
malloc
and free
函数声明于/usr/include/stdlib.h
编译器也使用这种方式生成调试信息,帮助您找到gdb中的文件路径和行号。