Visual studio c++ force rebuild of a specific file - c++

Is there a way to force Visual studio to rebuild a specific file on every build?
I have a version header with __DATE__ and __TIME__ and I want it automatically updated for each release.
I can do a prebuild event and a batch file to touch the file, just wondered if there was a feature to do this yet?

You can also delete the .obj file with a pre-build step. It will cause the compiler to rebuild your .cpp or .h file. Right click your project > Properties > Build events > Pre-Build Event > Command Line and add the following line:
del $(TargetDir)source.obj

From superuser, try adding a prebuild command:
copy /b filename.ext +,,
Where filename.ext is the path/name of the header you want touched. Caveat: I'm not certain VStudio always executes prebuild events, or only if it detects it actually needs to do a build.

There is a touch ms build task. Add it to the build depends on target. http://msdn.microsoft.com/en-us/library/37fwbyt5.aspx

I recently had this question using with the exact same underlying reason of wanting to always update __DATE__ and __TIME__ macros on build. No single answer here worked for me, but they were a great help.
As #Martin-Beckett mentioned in a comment, even pre build events won't run if nothing is perceived as having changed. The key is adding a post event to modify the file of interest. Then every time you hit Build it will be guaranteed to recompile that file as it will have changed after the last compilation.
Both the del $(IntDir)file.obj and copy /b file.h +,, methods worked for me. I'm partial to the copy, so for a step-by-step process:
Right-click project -> Properties -> Build Events -> Post-Build Event > Command Line and add the following line:
copy /b file.h +,,
I would recommend adding a Description for this as it comes up in the Build Output window. Now I can hit Build ten times in a row and that file is recompiled every time.

Related

How can I make sure that a file is compiled on every build n msvc? [duplicate]

In one cpp-file I use the __DATE__ macro to get the compile-date.
It gives me the date of the last compile of that file. But as the file is not changed very often, the date is old in most cases, sometimes several months.
What I actually want is the date of the last build of the project.
Is there an setting to force VS2010 to rebuild that single cpp-file on every compile of the project?
Regardless of changes in the file?
The only way I found until now is to modify the file or delete the created obj-file by an script before the build, I would prefer an solution inside VS if that is possible.
You could probably add a Pre-Build Step that touch (see this thread) the file?
To add a Pre-Build Step, open your Project Properties, then Configuration Properties > Build Events > Pre-Build Event then add the command line you want to have executed in Command Line.
Following the suggestion from Amitd, apparently you can also touch the file using PowerShell, see this for explanations.
As suggested by Adrian McCarthy in the comments below, deleting the .obj file would be preferable in the context where source control is used and you want to keep the .cpp read-only. Using the "macros" exposed by Visual Studio, deleting them can be made easy:
del $(TargetDir)sourcefile.obj
Quoted from Cheers and hth. - Alf as another way to achieve this
nmake (bundled with Visual Studio and the SDK) option /t does a touch, it was once the conventional way to do this for Windows programmers.
You can add the following pre-build step, were you simply touch the date stamp of the file. The +,, is a special flag to the copy command, telling it to update the timestamp of the file:
copy file.cpp +,,
As suggested by Adrian McCarthy, you can simply delete the object file every time you build the project.
Therefore, create a pre-build event invoking the del command. According to Microsoft, you can use the $(IntDir) macro to refer to the directory wher the object file is stored (you should not use the $(TargetDir) macro).
I had issues with the return code of the command (error MSB3073), therefore I changed the command to always exit with 0.
del $(IntDir)datefile.obj & exit 0
Create this build event in the project configuration, under Configuration Properties / Build Events.

How to always compile a cpp file with Eclipse?

I have included a unix timestamp __DATE__ in one of my cpp source files to indicate the build date of my program. Naturally with default setups it compiles the file only when I change it but now I'd like to have it always compiled. I tried to search through the project settings but it seems that this would need deeper understanding of the compiler. I'm using Eclipse with g++.
I tried to google and search for an answer but I found it difficult to find good keywords for this.
Also is there a markable difference wether I do this change for a header file instead of the source file?
Thanks for the answers.
You may create a 'pre-build' step which touch the file (so the time stamp is modified and force to recompile the file).
it seems that this would need deeper understanding of the compiler.
No, it must be done by the build system.
A very simple-minded way of doing it is as follows. Create a single file called date.cpp with the following content and nothing else:
#include <string>
std::string build_time() {
return __DATE__ " " __TIME__;
}
You can use it like this in some other source file
cout << "Built on " << build_time() << endl;
The build system keeps track of your changes and it will only recompile those source files that have changed (that are necessary to be recompiled). Since you are not changing date.cpp file, it won't recompile it. However, you can force it by right clicking on the project folder and then
Properties > C/C++ Build > Settings > Build Steps > Post-build steps
and adding the following line to the comand field on Linux:
rm -f <path to the Debug / Release directory>/date.o
where you put yours to <path to the Debug / Release directory>.
On Windows del /q seems to do the same as rm -f, please check.
Since we delete the generated object file date.o, the build system must rebuild it again with the current build date and time when you compile the your application.
It's likely that there are many other variants but the above is in my opinion simple enough and does the trick.

IAR - Adding pre-build command to delete an object file

In IAR embedded workbench IDE, I need to force the compilation of a file, every time I build the project (in order to recompile __DATE__ and __TIME__).
So I need to "touch" that file (i.e., delete the corresponding object file).
I went into the project options --> C/C++ Compiler --> Extra Options --> Use command line options.
In there, I entered a shell command for deleting that file, but without luck.
I tried several different ways of doing it, including to call a batch file.
Examples:
del "$OBJ_DIR$\mng_version.o"
cmd /c "del "$OBJ_DIR$\mng_version.o""
pre_build.bat
None of these worked.
Does anybody have any idea how to do this?
I use
Project options -> Build Actions
and enter a command into the Pre-build command line. This can be a batch file invocation. I have a utility that increments a build number declaration in a version file that is then re-compiled on every build.
This is the same on the MSP-430, ARM and Atmel AVR-32 versions of the IAR toolset.

force MS VS2010 to rebuild one cpp-file on every build

In one cpp-file I use the __DATE__ macro to get the compile-date.
It gives me the date of the last compile of that file. But as the file is not changed very often, the date is old in most cases, sometimes several months.
What I actually want is the date of the last build of the project.
Is there an setting to force VS2010 to rebuild that single cpp-file on every compile of the project?
Regardless of changes in the file?
The only way I found until now is to modify the file or delete the created obj-file by an script before the build, I would prefer an solution inside VS if that is possible.
You could probably add a Pre-Build Step that touch (see this thread) the file?
To add a Pre-Build Step, open your Project Properties, then Configuration Properties > Build Events > Pre-Build Event then add the command line you want to have executed in Command Line.
Following the suggestion from Amitd, apparently you can also touch the file using PowerShell, see this for explanations.
As suggested by Adrian McCarthy in the comments below, deleting the .obj file would be preferable in the context where source control is used and you want to keep the .cpp read-only. Using the "macros" exposed by Visual Studio, deleting them can be made easy:
del $(TargetDir)sourcefile.obj
Quoted from Cheers and hth. - Alf as another way to achieve this
nmake (bundled with Visual Studio and the SDK) option /t does a touch, it was once the conventional way to do this for Windows programmers.
You can add the following pre-build step, were you simply touch the date stamp of the file. The +,, is a special flag to the copy command, telling it to update the timestamp of the file:
copy file.cpp +,,
As suggested by Adrian McCarthy, you can simply delete the object file every time you build the project.
Therefore, create a pre-build event invoking the del command. According to Microsoft, you can use the $(IntDir) macro to refer to the directory wher the object file is stored (you should not use the $(TargetDir) macro).
I had issues with the return code of the command (error MSB3073), therefore I changed the command to always exit with 0.
del $(IntDir)datefile.obj & exit 0
Create this build event in the project configuration, under Configuration Properties / Build Events.

Embed a build number / build ID into a project in Visual Studio 2008

There has been confusion a few times with my testers somehow getting old builds of my project to test, and then reporting on fixed bugs.
How can I embed a build ID into my project? Current time of build, or simply starting at 1 and incrementing every time the program is built would work. Then in the game UI, the build number will be printed so there is no ambiguity as to which version of the software the tester is using.
I've googled around for an answer and asked on IRC, but everything I've found seems to pertain only to C#.
Any ideas? On Unix, I would just modify the Makefile, and have some oneliner insert the value I need into the source.
Right now my best idea is to figure out how the VS build process works, then write a python script to run first and edit the source to update the build number.
Ok, here is my quick and dirty and ugly solution.
I have a buildid.txt file, this is a text file with only an integer value for buildid.
I have increment-buildid.bat
#echo off
for /f %%a in (buildid.txt) do (
echo %%a
set /a num=%%a
)
echo %num%
set /a num += 1
echo %num% > buildid.txt
echo int buildid = %num%; > buildid.c
To muck about with the Visual Studio build process, right click solution in solution explorer, -> properties -> Build Events -> Pre-Build Events.
Now that I know how do do this, I can put in my zip + scp script in post build events for instant upload! (not sure of that's blocking or not, will have to test it)
edit: the upload is indeed blocking. that is, debugging does not start until the upload has finished. I just have another batch to upload using scp as I feel the need to. Double click it, and it is sent to remote server.
To solve the same problem, I wrote a small utility that generates a header file containing a date/time stamp in a #define. It runs as a pre-build step and the main project includes the generated header. Then you can include the stamp in a sign-on banner or the like.
In my projects I also generate a version resource, then move it into an .rc2 file (manually written resources) and modify it to include the generated header and update the version appropriately.