0

Let's say we've got a first program called Program1.exe which contains the necessary information to create and compile another application called Program2.exe. Actually it could also load that information from a txt file or whatever.

Googling, I've found that this is "easy" to do in C#, using Visual Studio:

How to programatically build and compile another c# project from the current project

Programmatically Invoke the C# Compiler

The problem is that I'm not using (and can't use) C#, but C++. Summing it up, my question is if that I can do this same thing using C++. I would prefer to do it without additional libraries, but if that's not possible, or if it's too hard to do, you can also recommend any library allowing it.

I think you'll probably have noticed it, but my goal is to use it under Windows so I don't care if it's not portable.

Thanks everybody.

Community
  • 1
  • 1
JuanGM
  • 25
  • 9
  • This looks like a very heavy handed technique. If you share your higher level goals, we might be able to give you advice on different strategies altogether. That being said, I think [llvm](http://llvm.org/) might be of help for creating executable files in runtime, even though I think it is still not as simple as in C#. – Magnus Hoff Sep 24 '14 at 13:54
  • @MagnusHoff Thanks for the advice Magnus. As you said, I don't think there would be methods as simple the ones provided by .NET Platform. I will take a look at that library you suggested me. – JuanGM Sep 24 '14 at 14:09

1 Answers1

3

It's trivial (if maybe a bit odd) for a C++ program to compile and run another based on code stored in a text file. Debugging that other program, however, isn't.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • But only if your program runs on a computer with a compiler/linker installed. Otherwise you might have to ship a whole C++ compiler inside your "trivial" program. – tgmath Sep 24 '14 at 14:02
  • @Paul As tgmath said, I can't ensure the presence of a compiler in the computer, so it should be capable of doing compiling the code itself. – JuanGM Sep 24 '14 at 14:06
  • +1, but to provide some actual leads for the OP - check out `system()` and the `execv`-family of functions to learn how to invoke another program, and use that to run the c++ compiler of your choice on the source code, which is easiest written to a temp file. That you happen to be running a compiler is of very little significance. You don't say that you want to execute `program2.exe` from `program1.exe`, but that can in turn be done in the same fashion. @user3869641 - you can check if any compilers have a library API you can use, but really that's a pretty unreasonable constraint.... – Tony Delroy Sep 24 '14 at 14:08
  • Make program1 the c++ compiler , and ship an nmake makefile and .bat? If program2 is running on windows also why do you need to compile it on the target machine? – skimon Sep 24 '14 at 14:16
  • @TonyD Thanks for the reply, Tony. Using a specially-designed API would be, from my point of view, a good option. The second step would be indeed to execute the created application from the original one, but that doesn't cause any problem. As you say, it would be much more easier to compile if I had the certainty that the computer has an installed C++ compiler, but I can't work with that assumption. – JuanGM Sep 24 '14 at 14:17