Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 898
MacGucky Avatar asked Nov 21 '12 16:11

MacGucky


People also ask

How do I rebuild a Visual C++ project With MSBuild?

To rebuild your project with the Visual C++ 11.0 Windows XP toolset, enter this command: MSBuild provides various ways to customize your build process. These articles show how to add custom build steps, tools, and events to your MSBuild project:

How to compile a single file in MSBuild?

you can msbuild have invoke devenv to compile a single file: devenv myproject.sln /Command "File.OpenFile myfile.cpp" /Command "Build.Compile" /Command "File.Exit" this does open the IDE window though, and will make it pretty hard to figure out if the compile actually succeeded or not.

How to build a single file in vs 2008 using vs 2010?

MSBuild in VS2008 uses VCBuild to do the actual work, and VCBuild has no option I know of to build a single file. (with VS2010 this has changed, there you can actually invoke a compile of a single file using something like "/t:ClCompile "p:/SelectedFiles="main.cpp")

What is an MSBuild project file?

*/ An MSBuild project file is an XML file that contains a project root element ( <Project> ). In the example project you'll build, the <Project> element contains seven child elements: Three item group tags ( <ItemGroup>) that specify project configuration and platform, source file name, and header file name.


2 Answers

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.
like image 160
emartel Avatar answered Oct 30 '22 15:10

emartel


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 +,,
like image 41
Kurt Van den Branden Avatar answered Oct 30 '22 16:10

Kurt Van den Branden