简体   繁体   中英

VIsual studio pre-build event

I have to build some C source files using Visual Studio (currently using VS2008) but there is a pre-build script that has to be run so the files are able to compile.

So this pre-build script has to modify the source files and here comes the problem: how can I use a pre-build script that modifies the src files.

If I modify the original files and the build failed the post build won't run so I won't be able to recover the files so I think the best way is to copy the files and then modify the copies and tell the compiler to compile the copies but I don't know if I can tell VS to compile some other files that are not included in the project instead of this that are in it.

So what is the best way to run such a pre-build script?

One way that you might be able to achieve this is to create a dummy project in your solution that just contains an empty .cpp file, and set this project as a dependency of the project whose files you want to modify. Then I think there's a couple of choices of how to set things up:

  • Set the post-build event of the dummy project to do the manipulation, or
  • Create a custom build rule for the cpp file (or some other dummy file in the project) to run the script

I'm not sure which of these two would do what you require in terms of whether they would need to run every time you build, or just the first time you build. If you use the 2nd approach then you could set up the output for the custom build rule to be a file that doesn't exist, and this would force the build rule to be executed every time. Because the dummy project is a dependency of the main project, it will go through the process of trying to build it every time you build the solution.

I'm not sure whether there's a way to manipulate which files to compile in a script. But you could also use the pre-build step to create a backup of the original files before preprocessing them. If the build fails, you can recover from the backups.

I remember doing this trick once, with a prepuild step which actually created one of the source files on the fly. I had to create it once, so I could add it to the project file, and afterwards I deleted this, since it would be automatically generated within the pre-build step.

EDIT: I think I would create an extra solution folder which contains the 'templates' for the source files and set the build action to none, so VS doesn't try to compile them. Create a prepuild script that copies those files to the right location and preprocesses them. You'll probably have to create some dummy ones to create the reference in project file, like I stated above. This way you can always access the original files from within the solution, and the prepuild step will prepare your files for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM