0

I have a c++ code that is compiled and can be executed.

Let's say the output file after compiling is executable.x.

I also have python script that can call this executable and run it.

pythonScript.py:

# lets say the path is absolute for simplicity.
file_path = 'C:/MyProject/code/executable.x'
# I need to pass an argument to main.cpp
subprocess.check_call([file_path, '-switch1'])

I can run the python script from terminal, and it runs the executable without any issue. Then there is a shell command to run the python script.

myShell.sh

#!/bin/sh

pwd
(cd pythonScriptDirectory && python3 pythonScript.py)
pwd

By running the sh script, it sets the working directory (like how I run python script from terminal), and then it runs the python script. It seems it also finds the executable.x, but it always return with some error.

Is there any suggestion what might be wrong here, or what would be the debugging approach.

The return value specifies the error code 3221225785 in decimal, which is C000 0139 Hex. My assumption is that the executable file can be selected to run, but a working directory issue causes that libraries being used by executable cannot be loaded.

Here is directory structure:

enter image description here

mousetail
  • 7,009
  • 4
  • 25
  • 45
Aryan Firouzian
  • 1,940
  • 5
  • 27
  • 41
  • 2
    *What* "error" do you get? – Some programmer dude May 16 '22 at 08:55
  • Return value is not zero or anything that I specified in c++ code. A big strange value is returned. – Aryan Firouzian May 16 '22 at 08:58
  • I put a print in first line of main.cpp. which is visible when running python by terminal. But never shows up when running the bash – Aryan Firouzian May 16 '22 at 09:01
  • Why are you running it in a subprocess, i.e. inside `( cd ... && python ...)` ? Also, you might make your `file_path` a raw string, `file_path = r'C:\....'` – Mark Setchell May 16 '22 at 09:12
  • A "strange big value" (on Windows) might indicate a crash. If you convert the value to hexadecimal, what is it? Can you try to search for it? – Some programmer dude May 16 '22 at 09:15
  • Without seeing the code, all we can tell you is that your C++ program does these things. Probably they happen when you run it outside of Python, too, but are less visible. Your `main` function should `return 0` when it succeeds (or actually `return EXIT_SUCCESS` for portability, but I think it's zero on any modern OS). – tripleee May 16 '22 at 09:17
  • The return value is `3221225785` in decimal, which is `C000 0139` in hex. My search for meaning of this hex doesn't result anything. 135 means `STATUS_DLL_NOT_FOUND` but is there any reference document that I check what 139 means? – Aryan Firouzian May 16 '22 at 09:25
  • @tripleee when I run the `executable.x` (cpp output) from the terminal directly, it works fine, and returns expected value (0 or whatever I specifies) – Aryan Firouzian May 16 '22 at 09:27
  • Actually 0x0139 is decimal 313, where `39` is exit code 57. But I think Windows uses the whole number. See https://stackoverflow.com/questions/53808442/vs-code-c-exited-with-code-3221225785 – tripleee May 16 '22 at 09:31
  • When you run the program directly from a terminal, are you doing it from the `pythonScriptDirectory` directory? Or from another directory where its required DLL's can be found? – Some programmer dude May 16 '22 at 09:32
  • 1
    Tangentially perhaps see also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) – tripleee May 16 '22 at 09:32
  • when I run it directly I change dir to directory of `executable.x`. But when running python I run it from `pythonScriptDirectory` – Aryan Firouzian May 16 '22 at 09:34
  • Try e.g. `subprocess.check_call([file_path, '-switch1'], cwd='C:/code')` (assuming `C:/code` is the correct location of the executable). – Some programmer dude May 16 '22 at 09:38
  • I added a directory picture to question and used `subprocess.check_call([file_path, '-switch1'], cwd='C:/MyProject/code')`, but I still get same error – Aryan Firouzian May 16 '22 at 09:51
  • Please include the full error text instead of just the return code – mousetail May 20 '22 at 07:24

0 Answers0