[RESOLVED] How to auto-generate .cpp and .h file in a pre-build event using Visual C++ 6.0

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

[RESOLVED] How to auto-generate .cpp and .h file in a pre-build event using Visual C++ 6.0

I'm want to use a pre-build event in Visual C++ 6.0 to run a script which will automatically generate a .cpp and .h files, but I am not sure how these files could add to project ... So when I build the project
1) script has to generate .cpp and .h files
2) Needs to add these files to project
3) Has to do a rebuild all.

can any point me in a right direction to achieve this ? any sample application also appreciated. Thank you very much in advance.

Re: How to auto-generate .cpp and .h file in a pre-build event using Visual C++ 6.0

Hi Thanks for your quick response. can we do this dynamically by using VC++ command line or something ? the reason why I am asking is I don't want them to add manually, once user say "build" they should automatically created and added to the project. Please bear with me ..

Re: How to auto-generate .cpp and .h file in a pre-build event using Visual C++ 6.0

You could do the followings steps:

- Create an empty project of the type you want to generate
- Add all files to that project (e. g. stdafx.h and stdafx.cpp when using PCH) which are not a matter of change
- Add your 'generator tool' to the 'Tools' menu of Visual Studio or - alternatively - use a separate (MFC-)application to get the basic informations for the new project to generate.
- If done so far your tool would make a copy of the 'empty template' project using the new project name given by the user.
- then it would create .cpp and .h files and add those files to the folder and to the copied .dsp file of the new project. You asily can see how VC6 adds new files to a .dsp by opening the .dsp in a text editor.
- finally you would invoke the msdev programmatically passing the new .dsp as a parameter. You can see how msdev can be started from commandline by typing 'msdev /?' at the command prompt.

Adding files to a currently opened project is not impossible but it requires COM or advanced Tools programming where I have little to no experiences with.

Re: How to auto-generate .cpp and .h file in a pre-build event using Visual C++ 6.0

As Mr Viggy said. the simple solution is to run the build once, and add the .cpp's and .h files once.
You can then make the .cpp file dependant on whatever file(s) is(are) serving as 'input' to generate the .cpp by specifying in the build settings of your input files that they're generating xxx.cpp.

If you have a set of input that is 'randomly' creating .cpp and .h files without predetermined filenames, then you will have to use a MAKE file.

Re: How to auto-generate .cpp and .h file in a pre-build event using Visual C++ 6.0

Originally Posted by S_M_A

If you add autogen.* to the project you don't have to bother about dependencies. Just run your generation tool as a pre-build step and the build process will detect what to rebuild.

Yes, but this has the nasty effect that the pre-build step will ALWAYS execute.
This will ALWAYS create new versions of the .cpp/.h files
This will ALWAYS cause a recompile, possibly of everything if the .h file is used everywhere.

The better solution is to add the files that are the input to create the .cpp/.h files to the project and give those a custom build step. Make sure you properly specify outputs, and if those outputs are also in the project, it'll work much better.
This way, if the input files haven't changed, the custom build won't run, won't generate the .cpp/.h and this won't cause a recompile of anything using those.

Suppose: Input.dat is input to create A.cpp and A.H

1) Add a A.cpp and A.h to the project, make dummy/empty files if you have to.
2) Add input.dat to the project.
3) Rightclick inpit.dat, click properties, and select Custom Build Step
Command Line: whatever command is needed that is going to read input.dat and make a.cpp and a.h
Outputs: $(Inputdir)a.h;$(inputdir)a.cpp
Additional depenencies: any extra files needed to run the command. It's often good idea to add the exe that reads input.dat here.

Re: How to auto-generate .cpp and .h file in a pre-build event using Visual C++ 6.0

Hi All,

Thanks for replies.

OReubens, I think your suggestion will be a good bet, but I have one more question. Say other files in the project needs to include a.h and use the defines in that header file will adding custom build generate this header before compiling the files which included a.h. I am not sure about this .. can you please tell me if my question doesn't make any sense.