binary object file changing in each build

家住魔仙堡 提交于 2019-12-17 17:06:19

问题


When compiling using G++ GNU compiler every time I do a build, without changing source code I get a different binary object file. Is there a compile option that will give me the same binary each time.


回答1:


Copied from the GCC man-page:

-frandom-seed=string
This option provides a seed that GCC uses when it would otherwise use random numbers. It is used to generate certain symbol names that have to be different in every compiled file. It is also used to place unique stamps in coverage data files and the object files that produce them. You can use the -frandom-seed option to produce reproducibly identical object files.

The string should be different for every file you compile.




回答2:


You should better use make. This way if your source didn't change, the compilation will be skipped, so the object files won't be changed.

Edit: after some thinking, it's possible to address your comment with the makefile which separates preprocessing and actual compilation. and some dirty tricks.

Example makefile:

all: source

source: source.i.cpp
    @cmp -s source.i.cpp source.i.prev || g++ source.i.cpp -o source
    @touch source
    @cp source.i.cpp source.i.prev

source.i.cpp: source.cpp
    @g++ -E source.cpp >source.i.cpp

Please note the the executable's time is changed, but the contents not (if you changed only the comments, not the actual code).



来源:https://stackoverflow.com/questions/4140329/binary-object-file-changing-in-each-build

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!