Martin Beckett
Martin Beckett

Reputation: 96157

Visual studio c++ force rebuild of a specific file

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?

Upvotes: 6

Views: 3487

Answers (4)

Billy
Billy

Reputation: 607

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.

Upvotes: 0

Kurt Van den Branden
Kurt Van den Branden

Reputation: 12973

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

Upvotes: 5

rerun
rerun

Reputation: 25505

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

Upvotes: 0

Nathan Ernst
Nathan Ernst

Reputation: 4590

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.

Upvotes: 4

Related Questions