How to run a C++ program inside another C++ program?

You will have to write your own compiler.

Consider: No normal OS supports what you want. You want both executables to run inside a single process, yet that process may or may not make OS calls depending on some weirdness inside the process which the OS doesn't understand at all.

This is no longer a problem with your custom compiler, as it simply will not create the offending instructions. It's similar to Java and .Net, which also prevent such OS calls outside their control.


A portable solution: Google Native Client

One possible Linux solution:

  1. Make AppArmor profile with "hats" (a "hat" is a sandboxing configuration to which the application can switch programmatically with libapparmor),
  2. have the main application create a "pipe",
  3. have the main application "fork",
  4. change into a "hat" corresponding to the child application,
  5. "exec" the child application,
  6. the main application and the child application communicate via the "pipe" created earlier.

Tags:

Linux

C++

Shell