[p4] Adding a Changelist # to C++ code...?

Peter Prymmer PPrymmer at factset.com
Thu Feb 21 11:42:33 PST 2008


perforce-user-bounces at perforce.com wrote on 02/21/2008 03:18:52 AM:

> Does anyone know of a way to integrate the last synced version number
> from Perforce into C++ code, such that it can be displayed on screen?

One approach with perforce could exploit keywords within the files 
(example shown below).

Before using this approach do note that keyword expansion by the SCM
tool is not universally appreciated (it can lead to odd diff and merge
problems).  You might consider confining it to one single header file if
you choose to adopt it instead of adding it to a large number of files.

// Demonstrate the use of perforce keywords as C++ preprocessor 
definitions in a source file.
// Add this demo file to the depot using:
//    p4 add -t ktext <filename>
// after the submit the macros are modified by perforce
// in the submitting client.  Hence within any other P4CLIENT that 
subsequently syncs
// the file out the macros will be expanded.

#include <iostream>

#define ID "$Id$"
#define DATE "$Date$"
#define DATETIME "$DateTime$"
#define REVISION "$Revision$"
#define AUTHOR "$Author$"
#define HEADER "$Header$"
#define CHANGE "$Change$"
#define FILE "$File$"

int main() {
    cout << "The author of this file was " << AUTHOR << endl;
    cout << "This file can be found in the depot at " << ID << endl;
}

I hope that helps.

Peter Prymmer



More information about the perforce-user mailing list