如何使用C中的特定头文件打印程序中使用的内置函数名称? [英] How to prints the built in functions name used in our program using a specific header file in C?

查看:159
本文介绍了如何使用C中的特定头文件打印程序中使用的内置函数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从特定的头文件中找到程序中使用的内置函数.

I need to find the built-in functions used in our program from a specific header file.

例如,我下面有C文件:

For example, I have the C file below:

#include<stdio.h>

int main()
{
    int a;
    scanf("%d",&a);
    printf("a = %d\n", a);
}

如果我将stdio.h头文件提供给任何命令,它需要提供如下输出:

If I given the stdio.h header file to any command, it needs to give the output as below:

scanf
printf

是否有内置命令来获取此信息?

Is there any built-in command to get this?

或者gcc或cc命令中可用的任何选项来获取此信息?

Or any options available in the gcc or cc command to get this?

推荐答案

如果将GCC用作编译器,则可以运行以下命令:

If you are using GCC as compiler, you can run this command:

echo "#include <stdio.h>" | gcc -E -

这将从stdio.h标头以及该标头包含的文件中打印许多行,依此类推.

This will print many lines from the stdio.h header, and from the files that are included by that header, and so on.

有些行看起来像#line …,它们告诉您以下几行的来源.

Some lines look like #line …, they tell you where the following lines come from.

您可以分析这些行,但是从它们中提取功能(解析)非常复杂.但是,如果您只想进行快速,不可靠的检查,则可以搜索这些行中是否包含单词scanfprintf.

You can analyze these lines, but extracting the functions from them (parsing) is quite complicated. But if you just want a quick, unreliable check, you could search whether these lines contain the word scanf or printf.

编辑

如注释中所建议,-aux-info更为有用,但仅在编译文件时有效,而在预处理时则无效.因此:

As suggested in a comment, the -aux-info is more useful, but it works only when compiling a file, not when preprocessing. Therefore:

cat <<EOF >so.c
#include <stdio.h>

int main(int argc, char **argv) {
  for (int i = 1; i < argc; i++) {
    fprintf(stdout, "%s%c", argv[i], i < argc - 1 ? ' ' : '\n');
  }
  fflush(stdout);
  return ferror(stdout) == -1;
}
EOF

gcc -c so.c -aux-info so.aux

可以使用objdump来确定程序中的函数调用,如下所示:

Determining the function calls from your program can be done using objdump, as follows:

objdump -t so.c

以上命令为您提供原始数据.您仍然需要解析这些数据并将其组合起来,以便仅为您提供与您的问题相关的数据.

The above commands give you the raw data. You still need to parse this data and combine it to only give you the data relevant to your question.

这篇关于如何使用C中的特定头文件打印程序中使用的内置函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆