Daniel YC Lin
Daniel YC Lin

Reputation: 15992

How could I query binary's source code version

My environment is Linux CentOS 6.2. And I've a source control system like svn/hg/git etc. My source code is C/C++.

I want to check in the build binary to keep which binary is release to customer.

And I assume build binary's checksum will different when source code changed. So, I could reverse trace which binary is build from which version.

Is it possible, what's the tricks I must follow?

I've seen some executable display the revision when execute with -version option. But I'm wonder how to prevent write wrong -version string into the executable.

If I keep a md5.txt and check-in it instead of check in binary. How could I make sure I can build the same md5 executable again?

Sorry, for clearing my question and preventing another unexpected answer, I prefer a answer like:

  1. Keep a md5sum.txt in scm when release a new version to user.
  2. Keep binary separate from your SCM.
  3. To rebuild the same md5sum binary you should make sure
    • write symbol into binary when make(eg. by -DVERSION="1.x")
    • show the VERSION string to user
    • remove all $Id, that let your SCM run slower.
    • keep same CPU & OS & compiler & library environment
    • ...

Upvotes: 1

Views: 983

Answers (3)

Lazy Badger
Lazy Badger

Reputation: 97280

If you'll use Subversion, SvnRev will do most work for you (no md5 in repos, repo hold sources, binary - resource with revision-id)

For Mercurial, you can get idea for version sting from VersioningWithMake wiki, and in order to get string like result of git describe, instead of simple template {node|short} for HGVERSION you can use something as {latesttag}+{latesttagdistance}:{node|short}, showing (example) 1.3+11:8a226f0f99aa

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59987

  1. Create strings within a .cpp file as thus:

    static const char version[] = "@(#) $Id$";

    where $Id$ is obtained from SVN

  2. Use the what command (see the manual page). It will obtain these strings from the binary so you can check.

Upvotes: 3

Multimedia Mike
Multimedia Mike

Reputation: 13216

Is this an executable or a shared library? If the latter, you could export a function that would return the version (number, string, your choice). Then dlopen(), dlsym(), and execute the function.

For executable ELF binaries, you might be able to implant some data in the binary that can be queried using the 'nm' utility.

Upvotes: 0

Related Questions