OS X上的gprof问题:[program]不属于主机架构

时间:2009-07-09 02:28:36

标签: macos gcc gprof

我在OS X上运行gprof时遇到问题。文件test.c是:

#include <stdio.h>

int main() {
  printf("Hello, World!\n");
  return 0;
}

我的终端看起来像:

$ gcc -pg test.c
$ gcc -pg -o test test.c
$ ./test
Hello, World!
$ gprof test
gprof: file: test is not of the host architecture

编辑:此外,它不会生成文件gmon.out

这里发生了什么?

3 个答案:

答案 0 :(得分:9)

这里的一系列事件应该如下工作:

  1. 使用-pg选项
  2. 编译代码
  3. 使用-pg选项
  4. 链接代码
  5. 运行程序
  6. 程序生成gmon.out文件
  7. 运行gprof
  8. 问题是第4步永远不会发生。关于这一具体失败的信息非常少。过去几年普遍的共识似乎是苹果宁愿使用鲨鱼,而且他们在修复错误方面非常松懈等等gprof

    简而言之:安装Xcode,man shark

答案 1 :(得分:4)

不幸的是gprof在Mac OS X上不起作用。您可能希望改用Shark。它是/Developer/Applications/Performance Tools/Shark中的开发人员工具的一部分。

更新:好像gprof正在使用最新的开发者工具在Mac OS X 10.6(Snow Leopard)上运行。

答案 2 :(得分:3)

听起来test是使用gprof不期望的架构构建的。请尝试以下方法:

$ cat > test2.c
#include <stdio.h>
int main() { printf("test\n"); return 0; }
^D
$ gcc -arch i386 -pg -o test2 test2.c
$ file test2
test2: Mach-O executable i386
$ ./test2
test
$ gprof test2
... bunch of output ...
$ gcc -arch ppc -pg -o test2 test2.c
$ file test2
test: Mach-O executable ppc
$ ./test2
test
$ gprof test2
gprof: file: test2 is not of the host architecture
$ arch -ppc gprof test2
... same bunch of output ...

较新的MacOS支持从IBM PPC和Intel x86架构运行可执行文件。一些工具链似乎对此有点密集。 Gprof似乎期望可执行文件在本机架构中。但是,如果使用arch实用程序强制执行非本机体系结构,则它似乎正常工作。不久前有一个discussion about this in another context。我在那里提供了一些有用的链接和更多信息。