1

I have gone through various questions on the internet to execute a cpp file from python or another cpp file, however, I faced some issues that I would like to share.

Case 1.

a) When I try to run a cpp file from python as described here, I get this error hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified. However, a.out is created inside the same directory but it still says no file found.

Case 2.

a) When I try to run .cpp file from another cpp file as described here, I don't get an error but let's say I want to run a.cpp from b.cpp so first I need to build a.exe and then call system("a.exe") from b.cpp code, but I want something as shown below since I don't want to manually create an executable file for a.cppevery time.

// b.cpp
method("a.cpp");
OR
command to create a.exe 
system("a.exe") 
7
  • What do you mean by "execute a cpp file"? C++ code is not executed, it is compiled and linked with other C++ code to produce an executable. Are just trying to call a function defined in another .cpp file? Can you show minimal reproducible example of what you are trying to achieve? Commented Apr 4, 2022 at 8:06
  • You can't "run" or "execute" C++sourc files. Source files must be built and linked into an executable program before you can run the generated executable. If you want to avoid rebuilding every time, perhaps use a tool such as Make to build and execute? Commented Apr 4, 2022 at 8:06
  • c++ is a compiled language, you can't avoid creating an executable Commented Apr 4, 2022 at 8:06
  • 1
    With that said, there do exists a couple of C++ interpreters which can compile and link C++ source files in memory, and run them from memory. Commented Apr 4, 2022 at 8:08
  • @Yksisarvinen I am replicating the same thing stackoverflow.com/a/58422966/13248217 as described here, I am not calling any function defined in another .cpp file Commented Apr 4, 2022 at 8:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.