[RESOLVED] How to auto-generate .cpp and .h file in a pre-build event using Visual C++ 6.0
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

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

  1. #1
    Join Date
    May 2009
    Posts
    11

    [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.

    SKS
    Last edited by kishoresajja; August 11th, 2010 at 04:48 PM.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

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

    Manually add them once, then save the project. Deleting the files on the disk doesn't automatically remove them from the project.

    Viggy

  3. #3
    Join Date
    May 2009
    Posts
    11

    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 ..

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile 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.

    Regards, Alex

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,981

    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.

  6. #6
    Join Date
    May 2009
    Posts
    11

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

    Hi All,

    Thanks for replies. I could add them manually for the first time but I don't know how to add dependencies between the files in VC++ 6.0 project. Can any please point me in right direction.

    In other words say I have use.cpp and use.h which will include autogen.h and use defines in autogen.cpp then how can I add dependencies for use.cpp and use.h?

    autogen.h and autogen.cpp are files which were auto generated. And these files won't change until I change their base header from which these two files generated.

    Thanks
    SKS

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

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

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,981

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

    Quote Originally Posted by S_M_A View Post
    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.

  9. #9
    Join Date
    May 2009
    Posts
    11

    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.

    Thanks
    SKS

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

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

    Maybe I did read the posts without 'really read them' so I didn' realize what your solution ment OReubens. All in all, it's a good solution.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center