-1

It is possible for c++ program to open and run another c++ program like in python using the function"execfile('filename.py')".

I know it is possible to compile the the c++ program to exe and the run the exe file using the function "system("start main.exe")". However, is there a method that would work for Windows, Linux and Mac without changing the code and without recompiling the files?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
MeHow
  • 133
  • 1
  • 1
  • 8
  • 4
    Normal C++ is not designed to be interpreted... Python is not the same as C++ – rlam12 Apr 22 '16 at 14:42
  • 1
    The simple answer is no, the files would need to be recompiled. The complex answer: yes, it is *possible*, search the internet for "c++ interpreter". You would need to either include the interpreter functionality in your program or install the interpreter along with your program. – Thomas Matthews Apr 22 '16 at 14:54

2 Answers2

4

One-word answer: No.

There are C++ interpreters, so at least to some extent it is possible in principle to run a C++ program "without recompiling the files".

However, a typical Windows installation does not have any such thing installed (nor a C++ compiler) so you are out of luck unless you are willing to include a C++ interpreter as part of your own code.

And Windows, Mac and Linux all operate in sufficiently different ways that you will certainly not be able to make everything work without platform-specific code.

If you (1) are willing to have a pile of platform-specific code and (2) are willing to include a complete C++ compiler or interpreter in whatever you're building, then of course you can do it. But I don't get the impression that that's what you're after.

Community
  • 1
  • 1
Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
1

No, it is platform-specific.

For example, already between Windows and Linux, there is a difference, system() and the family of exec() functions.


As @makadev said, "actually, the function is less of a problem, c++ can use [std::system] which probably wraps the system calls exec or system, its more about the not recompile and typical shell differences"

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • actually, the function is less of a problem, c++ can use [std::system](http://en.cppreference.com/w/cpp/utility/program/system) which probably wraps the system calls `exec` or `system` , its more about the _not_ recompile and typical shell differences – makadev Apr 22 '16 at 14:45