1

I'm new to C++ and I want to run this C++ code using python.

The C++ code

#include <stdio.h>

int main() {
    printf("hello this is inside c++ ");
    return 0;
}

The Python Code

import subprocess

print("in python file")
subprocess.call(["gcc", "c_file.cpp"])
subprocess.call("./a.out")
print("task is done")

The error I'm getting when I run using the python file

    Traceback (most recent call last):
  File "C:\Users\Amiru Randil\PycharmProjects\pythonRunC++Code\main.py", line 5, in <module>
    subprocess.call(["gcc", "c_file.c"])
  File "C:\Python382\lib\subprocess.py", line 349, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Python382\lib\subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Python382\lib\subprocess.py", line 1416, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
15
  • changed it but still getting the same error Commented Mar 23, 2021 at 5:23
  • you need to put the full path to gcc ... you can find this with where gcc when you run it from cmd.exe Commented Mar 23, 2021 at 5:25
  • Still not working. Getting the same error Commented Mar 23, 2021 at 5:30
  • 1
    a. out is the default executable name generated by the gcc. So it's not a file, it's a command to run the C++ file Commented Mar 23, 2021 at 5:51
  • 1
    I highly advise against using Python as a build system. Probably the most versatile and well used build system is CMake, which can be configured for pretty much everything under the sun. It takes a bit to learn to use properly, but it's so powerful. Commented Mar 23, 2021 at 7:41

2 Answers 2

1

As your error shows you are in windows so the executable will not be called a.out it will be a.exe also ./ is not used in windows.

import subprocess

print("in python file")
subprocess.call(["gcc", "c_file.cpp"])
# change ./a.out to a.exe
subprocess.call("a.exe")
print("task is done")
Sign up to request clarification or add additional context in comments.

1 Comment

Also, an automated Python script is not what should be used as a build system. CMake is the most prominent build system for a reason
1

Can you compile and run the cpp file standalone? That is, are you able to run it normally? Because I do not see any error in your code apart from thinking that you have not installed the c++ compiler.

Here is my c_file.cpp:

#include <stdio.h>

int main() {
    printf("hello this is inside c++\n");
    return 0;
}

and this is my run_c.py:

import subprocess

print("in python file")
subprocess.call(["g++", "c_file.cpp"])
subprocess.call("./a.out")
print("task is done")

When I run it, I get the correct output printed. Like so:python run_c.py

in python file
hello this is inside c++
task is done

Comments

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.