Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-generating C++ code in a pre-build event using Visual Studio

I'm trying to use a pre-build event in Visual Studio (VS 2005 to be specific) to run a Python script which will automatically generate a .cpp file. The trouble I'm running into is that the compiler doesn't seem to know that this file is dirty and needs to be rebuilt until after the build has finished, which means that I need to build the solution twice -- once to generate this file, and then once more once so that this file actually gets compiled.

Without knowing much about the inner workings of the C++ compiler, my naive guess is that it makes a list of dirty files which need to be recompiled before the pre-build event runs, so it misses this auto-generated file, as it hasn't been touched until after the pre-build event.

Is there any way to inform the compiler that it needs to recompile this file if the pre-build event changes it?

like image 864
J. Kyle Pittman Avatar asked Feb 11 '10 16:02

J. Kyle Pittman


People also ask

How do I create a post build event in Visual Studio?

In the Post-build event command line box, specify the syntax of the build event. Add a call statement before all post-build commands that run . bat files. For example, call C:\MyFile.

What is PreBuildEvent?

Pre/Post build events are useful when we wish to perform some operation before/after a project is built. These operations are nothing but the Shell commands being used from the command line. A build event can be formed using a single, multiple, or conditional commands. Introduction.

How do I open a build event in Visual Studio?

Go to Solution Explorer and right-click on the project then select Properties then go to the Build Events tab.

How do I get build command in Visual Studio?

To compile source files from within the Visual Studio IDE, choose the Build command from the Build menu. When you build project files by using the Visual Studio IDE, you can display information about the associated vbc command and its switches in the output window.


2 Answers

I use msvc 6.

Try...

Put the python script into the project
give it a custom build step that invokes python on it,
to create the cpp file.

Add the cpp file to your project and do a rebuild all.

This is how we do it with the Oracle Pro*C preprocessor. It works fine.

like image 153
EvilTeach Avatar answered Sep 21 '22 17:09

EvilTeach


It's not something that I've ever done but you could try invoking the compiler (cl.exe) directly from your pre-build event.

like image 20
Paul Avatar answered Sep 23 '22 17:09

Paul