c - 如何从 C 执行 nm 并捕获输出

标签 c exec fork pipe nm

我正在尝试编写一个程序来检查另一个程序的全局变量的大小。让我们称我的程序为“check”,被检查的程序为“a.out”,这意味着我将像这样运行我的程序:

./check a.out

我想我可以使用命令找到这个值:

nm -t d -S a.out | grep ' B ' | awk '{print $2}'

我知道您可以使用 fork/execl 从程序内部执行程序,但我在编写 IO 管道时遇到了一些麻烦。

使用一些简单的 strcpy/strcats,我有一个包含上述字符串的 c 字符串,我可以使用 execl 调用它,但它似乎没有打印任何内容。现在它看起来像这样:

// Code setting char command[1024] equal to the above string
int pid;
if(pid = fork()){ // Parent
    // Need some pipes here?
}
else{ // Child
    execl(command, NULL);
}

任何人都可以指出正确的方向来设置这些管道吗?

最佳答案

pubs.opengroup.org/onlinepubs/009696799/functions/popen.html可能是你需要的。 – 垫

看起来我可以让 popen 做我想做的事。我仍然需要解析输出并将其转换为整数,但看起来这可以完成工作! – nchen24

关于c - 如何从 C 执行 nm 并捕获输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20417397/

相关文章:

c - execl() 导致应用程序崩溃

perl - 通过 exec 运行外部命令保留颜色

c - 原始克隆系统调用

c - execvp-ls : fts_open: No such file or directory

c - 有没有办法让 dirent 的 ent->d_name 作为 w_char * ?

c - 心脏出血错误 : Why is it even possible to process the heartbeat request before the payload is delivered?

c++ - 您使用什么工具来分析您的 C++ 应用程序的挂钟?

c - 数据包分层传输

c - 为什么在 C 中重定向 stdin 不起作用?

c - C 中的多管道