如何找出C源代码中哪个头文件存在哪些函数?
我有以下代码:
#include #include int main() { printf("Checking\n"); exit(0); }
在源代码之前,我有两个函数,一个是stdio.h头文件中的printf()。 第二个是stdlib.h头文件中的exit()函数。
现在我想要这个程序的输出是:
printf()函数存在于stdio.h中
exit()函数存在于stdlib.h中
有没有办法找到这个?
实际上没有可移植的方法,因为标题文本根本不需要存在于文件中! 在#include
的标准下完全可以接受修改环境而不引用特定的实际头文件,因此,在这种情况下,您无法轻松访问程序中的信息。
在查明标题文本是否可用方面,这可能包括简单的文本搜索(可能有误报,因为可能有关于math.h
头文件中的printf
的注释)到完整的C-aware解析器(可能很复杂)。
或者,你可以(手动)引用实际标准,因为那里有那些细节,a:
7.21.4.1 The remove function Synopsis #include int remove(const char *filename);
来自C11的这个小片段意味着可以在stdio.h
找到remove
函数。
而且,如果您仍想在程序中执行此操作,只需将标准中的所有知识收集到文件或数据结构(如关联数组)中,然后编写代码以查找给定标识符,其行如下:
#include #include const char *lookFor (const char *ident) { // These should have ALL identifiers and their // equivalent headers. static const char const *identifier[] = { "remove", "strcpy", ... }; static const char const *header[] = { "stdio.h", "string.h", ... }; assert (sizeof(identifier) == sizeof(header)); for (size_t i = 0; i < sizeof(header) / sizeof(*header); i++) if (strcmp (ident, identifier[i]) == 0) return header[i]; return NULL; }