1

I have successfully extended my python code in C following this:

Call a Python function from within a C program

I have compiled the code and created a .dll. However, when I opened it with the dependencywalker I have seen that it still requires the python code. I want the dll to be standalone, so it doesn't depend on the script .py. I thought that using the tag -static in g++ would be enough but it doesn't work for me.

3
  • You want a Python subroutine in your C-program, or you want a C-subroutine in your Python program? Commented Apr 25, 2021 at 9:29
  • @LorinczyZsigmond I want the Python subroutine in my compiled C-Program, so I don't need to have the python source code in the same folder. Commented Apr 25, 2021 at 18:09
  • Then find a Python-to-C or Python-to-machine_code translator. Commented Apr 27, 2021 at 13:17

1 Answer 1

2

The Python documentation has a chapter on Extending and Embedding Python with C.

With good enough coding conventions this can by applied to C++ code. You will use extern "C" to declare functions coded in C++ callable from C.

Be careful to avoid throwing a C++ exception to Python code.

On Linux, see also dlopen(3) and dlsym(3) and the C++ dlopen mini howto.

Python is open source. You should consider downloading its C source code and studying it.

Be aware of the GIL in Python. Multi-threading C++ code will be tricky.

Consider also generating C or C++ code with tools like RefPerSys or ANTLR, or writing in Python your C or C++ code generator.

Perhaps use static source code analyzers (like Frama-C or Bismon) on your C source code.

Be aware that C and C++ are different programming languages. See this and n1570 and n3337 or better.

Sign up to request clarification or add additional context in comments.

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.