set( filename "build-number.txt" )# check for the previous build numberif ( EXISTS "${filename}" ) # read build number from file file( READ "${filename}" build_number LIMIT 10 )else() # no file with build number, assuming very first build set( build_number 0 )endif()# increment build numbermath( EXPR build_number "${build_number} + 1" )# save new build number into the same filefile( WRITE "${filename}" "${build_number}" )
cmake_minimum_required( VERSION 2.8 )find_package( Qt4 REQUIRED )include( "${QT_USE_FILE}" )add_executable( test main.cpp )target_link_libraries( test ${QT_LIBRARIES} )# add target that will increment build numberadd_custom_target( increment ALL "${CMAKE_COMMAND}" -E chdir "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_COMMAND}" -P "increment.cmake")add_dependencies( test increment )
C++ (Qt) #include <QApplication>#include <QWidget>#include <QFile>#include <QTextStream> int main( int argc, char ** argv ){ QApplication app( argc, argv ); // read build number int buildNumber = -1; { QFile file( "build-number.txt" ); if ( file.open( QIODevice::ReadOnly ) ) { QTextStream ts( &file ); const QString buildNumberString = ts.readLine(); bool ok; const int readBuildNumber = buildNumberString.toInt( &ok ); if ( ok ) buildNumber = readBuildNumber; } } const QString BuildNumberTemplate = QString( "Build number: %1" ); const QString title = buildNumber == -1 ? BuildNumberTemplate.arg( "Unknown" ) : BuildNumberTemplate.arg( buildNumber ); QWidget w; w.setWindowTitle( title ); w.show(); return app.exec();}
#define _RELEASE_BUILD_NUMBER_ ${build_number}
...# generate header with build versionconfigure_file( "build_number.h.template" "build_number.h" )
C++ (Qt)#include <QApplication>#include <QWidget> #include "build_number.h" int main( int argc, char ** argv ){ QApplication app( argc, argv ); QWidget w; w.setWindowTitle( QString( "Build number: %1" ).arg( _RELEASE_BUILD_NUMBER_ ) ); w.show(); return app.exec();}
Во-первых, привязывать функционал к IDE - моветон.