1

I want to call a compiled C++ executable from another script and also need the value returned by the executable. Using system() doesn't return the function output and am not able to find any other alternative. Is this possible without having to write to a file and parsing from it?

I am working with 2 different libraries and want to use one of the libraries from another. So, I thought of doing it by creating the executable and calling it from the script of the other library. I also tried to compile both together by adding the header file directories of the other library during make but get this error ImportError: /.conda/envs/myenv/lib/python3.6/site-packages/hnswlib.cpython-36m-x86_64-linux-gnu.so: undefined symbol: gzopen64 which I am not able to understand how to fix.

Bhavay
  • 41
  • 2
  • See if [`popen`](https://man7.org/linux/man-pages/man3/popen.3.html) is available to you. [Here's a good example](https://stackoverflow.com/a/478960/4581301) – user4581301 May 28 '21 at 05:16
  • You can use fork() to create a child process then use exec family to call the executable bin. pipe() can be used for communication between two processes. Also waitpid() to wait for the child process complete and check the returned status – nhatnq May 28 '21 at 06:22

1 Answers1

1

You haven’t specified your OS, but if you’re on a Linux/UNIX environment you can use the standard POSIX function popen, which starts a program and gives you a file handle to the program’s input/output streams. You may find the full documentation for this function here: https://pubs.opengroup.org/onlinepubs/009695399/functions/popen.html

nneonneo
  • 171,345
  • 36
  • 312
  • 383