0

Basically, I have a program that generates a couple of .cpp files, which are included in a Visual Studio project. What I want is to have my program then tell Visual Studio to build that project, now that the .cpp files have been generated.

I can use system() to call MSBuild or devenv to do it. Those are both misbehaving at the moment but I can make them work. But ultimately, what I really want is to interface with Visual Studio/MSBuild directly in my C++ code, so I could do things like have it return the path to the output to me directly, or what have you.

Is there any way to do that? Every time I do a search on "building a Visual Studio project within C++" or similar, of course, I just get basic tutorials on setting up a project and building it through the IDE (or, at best, through the command line).

KRyan
  • 7,308
  • 2
  • 40
  • 68
  • 1
    These may help your issues http://stackoverflow.com/questions/333159/how-to-set-msbuild-vc-directories or http://stackoverflow.com/questions/280559/how-to-get-cmd-line-build-command-for-vs-solution/280584#280584 – kenny Dec 02 '12 at 17:26

3 Answers3

1

Use CreateProcess and use redirection of the stdout to pipe the output to a file. You can then programmatically read and evaluate this file.

MS example: http://support.microsoft.com/kb/190351
Maybe this helps: How do I redirect output to a file with CreateProcess?

Community
  • 1
  • 1
cxxl
  • 4,939
  • 3
  • 31
  • 52
  • Ultimately I can figure out the output path from the inputs I'm getting, but that's *really* useful stuff to know. – KRyan Dec 02 '12 at 17:46
1

you can run the generator from studio as a pre-build-event. that is the way MS think we should do it. additionally within a solution you can chain projects via dependency. again the MS style. alternativly you can do a real make system like CMake.

stefan
  • 3,681
  • 15
  • 25
  • Well, that won't help me because the generating process needs to be run every time the program is run, not merely every time it is built. Still, it's good to know what Microsoft recommends. – KRyan Dec 02 '12 at 17:46
  • what is the difference beween run generator -> build generated project and trigger build -> pre-build runs generator -> build generated project – stefan Dec 02 '12 at 17:55
  • Building requires that the user opens up the solution in VS and builds it; boss wants this to just be something someone can run. – KRyan Dec 02 '12 at 17:56
  • you can build a ms solution via command line as well. make a nice start script. unless your boss is generating these files in a religious ceremony from a fancy gui :-) – stefan Dec 02 '12 at 18:03
  • Oh, no, I need more than just a batch script, because I have to do some processing on the inputs and the like. It looks like `system` is the way to go. – KRyan Dec 02 '12 at 18:04
0

You can launch any command line from within .NET by using System.Diagnostics.Process.

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

cowboydan
  • 1,062
  • 7
  • 15
  • This sounds promising but I'm not sure how to use it. Could you expand on your answer some? – KRyan Dec 02 '12 at 17:47