我正在尝试在 Solaris 11 上使用 Solaris Studio 12.x 构建一个简单的 hello world 程序。如果我从两个不同的文件夹编译相同的文件,我会在目标文件中得到一个差异,如 diff 命令或 cmp 所示命令。
对两个目标文件 (od -x) 进行十六进制转储并进行比较,并使用可以将十六进制转换为 ASCII 的在线工具进行比较,发现存在差异是因为目标文件包含编译所在的当前目录发起。
问题是如何告诉编译器不要在目标文件中包含当前目录的信息。尝试了几个优化选项 - 无法解决此问题。
以下是我使用的代码和命令:
bash-4.1$ cat a.cpp
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
bash-4.1$ ls
a.cpp temp1 temp2
bash-4.1$ cd temp1
bash-4.1$ /opt/SunProd/studio12u3/solarisstudio12.3//bin/CC ../a.cpp
bash-4.1$ cd ../temp2
bash-4.1$ /opt/SunProd/studio12u3/solarisstudio12.3//bin/CC ../a.cpp
bash-4.1$ cd ..
bash-4.1$ diff temp1/a.out temp2/a.out
Binary files temp1/a.out and temp2/a.out differ
bash-4.1$ cmp temp1/a.out temp2/a.out
temp1/a.out temp2/a.out differ: char 5968, line 24
bash-4.1$ od -x temp1/a.out > temp1/a.hex
bash-4.1$ od -x temp2/a.out > temp2/a.hex
bash-4.1$ diff -c temp1/a.hex temp2/a.hex
... shows hex diff (two sets) ...
最佳答案
编译器正在嵌入对调试器有用的信息。此信息默认位于 dwarf 中格式。
您可以使用 dwarfdump命令为二进制文件提取此信息。如果比较为每个文件生成的输出,您会看到编译原始二进制文件的目录存储在 dwarf header 中的几个位置,例如:
# diff */*.dwarf
9c9
< DW_AT_comp_dir "/tmp/temp1/"
---
> DW_AT_comp_dir "/tmp/temp2/"
29c29
< 2: N_CMDLINE 0x0,0x0,0x0 "/tmp/temp1/; /opt/solarisstudio12.4/bin/CC ../a.c"
---
> 2: N_CMDLINE 0x0,0x0,0x0 "/tmp/temp2/; /opt/solarisstudio12.4/bin/CC ../a.c"
剥离二进制文件 (strip a.out
) 应该删除该 header 并使两个编译文件相同。
如果您使用较早版本的编译器或使用-xdebugformat=stabs
编译器选项,调试信息将存储在stabs 中。格式而不是矮人。在这种情况下,提取它的命令是 dumpstabs并会发现与编译目录相关的类似差异,例如:
.stabs "/tmp/temp1/; /opt/solarisstudio12.4/bin/CC -xdebugformat=stabs ../a.c",N_CMDLINE,0x0,0x0,0x0
关于c++ - Solaris Studio 在目标文件中添加当前目录信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36589690/