user1479670
user1479670

Reputation: 1291

why does the compilation of identical files in different directories yield different results?

I noticed that the object file generated from a source file by g++ in one directory is different from the one generated from an identical source file in another directory:

tools_io $ diff -s MoveStats.h ../kernel/MoveStats.h
Files MoveStats.h and ../kernel/MoveStats.h are identical
tools_io $ diff -s MoveStats.cpp ../kernel/MoveStats.cpp
Files MoveStats.cpp and ../kernel/MoveStats.cpp are identical
tools_io $ g++ -c MoveStats.cpp -I ../common -o MoveStats.o -g -Wall -fopenmp
tools_io $ cd ../kernel
kernel $ g++ -c MoveStats.cpp -I ../common -o MoveStats.o -g -Wall -fopenmp
kernel $ cd ../tools_io
tools_io $ diff MoveStats.o ../kernel/MoveStats.o
Binary files MoveStats.o and ../kernel/MoveStats.o differ
tools_io $ ls -l MoveStats.o ../kernel/MoveStats.o
-rw-r--r-- 1 jody morpho 20632 Apr 13 11:22 ../kernel/MoveStats.o
-rw-r--r-- 1 jody morpho 20632 Apr 13 11:21 MoveStats.o
tools_io $ nm MoveStats.o > nmlocms.txt
tools_io $ nm ../kernel/MoveStats.o > nmkerms.txt
tools_io $ diff -s nmlocms.txt nmkerms.txt
Files nmlocms.txt and nmkerms.txt are identical

I use g++ 4.9.4 on gentoo:

tools_io $ g++ --version
g++ (Gentoo 4.9.4 p1.0, pie-0.6.4) 4.9.4
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

What could cause the two object files to be different?

Can i trust them to be functionally equal?

Upvotes: 0

Views: 30

Answers (1)

Manthan Tilva
Manthan Tilva

Reputation: 3277

As you are compiling file with -g option, compiler add lots of debug information in section header.

If you drop -g option then you will get exact match.

Try to use objdump to visualize what is there inside object file.

And without -g if you have same code but just file name are different even then you will get difference, due to SYMBOL TABLE of object file also contain name of source file from which it was generated.

UPDATE: you can also try readelf even more information

Upvotes: 1

Related Questions