Skip to main content
 首页 » 编程设计

optimization之LLVm clang,错误:带有-fprofile-instr-use的无效文件格式(坏魔术)

2025年05月04日27mq0036

标志“ -fprofile-instr-use”会产生以下错误。
即使我们使用cmake或configure生成llvm,clang和compile-rt,也会发生此问题。
请让我知道您的输入来解决此问题

错误:无法读取配置文件:无效的文件格式(坏魔术)

谢谢,

重现此问题的步骤:

$ clang -O2 -fprofile-instr-generate hello.c -o c1.out 
 
$ ls -rlt 
-rw-r--r-- 1 root root       70 Jul 11 10:10 hello.c 
-rwxr-xr-x 1 root root    15793 Jul 11 10:10 c1.out 
-rw-r--r-- 1 root root 12203204 Jul 11 10:10 gmon.out 
 
$ ./c1.out 
Hello world 
 
$ ls -rlt 
-rw-r--r-- 1 root root       70 Jul 11 10:10 hello.c 
-rwxr-xr-x 1 root root    15793 Jul 11 10:10 c1.out 
-rw-r--r-- 1 root root 12203204 Jul 11 10:10 gmon.out 
-rw-r--r-- 1 root root      104 Jul 11 10:10 default.profraw 
 
$ clang -O2 -fprofile-instr-use=default.profraw hello.c -o c2.out 
error: Could not read profile: Invalid file format (bad magic) 
1 error generated. 
 
Clang version (July 10th-2014 build from stage): 
$ clang -v 
clang version 3.5.0 (llvm.org/git/clang.git 5f9d646cba20f309bb69c6c358996d71912c54cd) (llvm.org/git/llvm.git dc90a3ab8ffc841a442888940635306de6131d2f) 
Target: x86_64-unknown-linux-gnu 
Thread model: posix 
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8 
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.2 
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9 
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.0 
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8 
Candidate multilib: .;@m64 
Candidate multilib: 32;@m32 
Selected multilib: .;@m64 
 
OS:  Ubuntu 14.04 
 
LLVM configure: ../llvm/configure --enable-profiling --enable-optimized --enable-shared --disable-debug-runtime --enable-targets=x86  

请您参考如下方法:

事实证明,此处概述了步骤3:http://clang.llvm.org/docs/UsersManual.html#profiling-with-instrumentation

即使您只使用1个输出文件,也是必需的。 “将多个运行中的配置文件合并并将“原始”配置文件格式转换为clang期望的输入”听起来像只有在有多个配置文件的情况下才应该这样做,但您需要无条件地这样做。