binary object file changing in each build

2019-01-15 19:20发布

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.

标签: c++ g++
2条回答
闹够了就滚
2楼-- · 2019-01-15 19:58

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).

查看更多
疯言疯语
3楼-- · 2019-01-15 20:14

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.

查看更多
登录 后发表回答