-2

I have a moderate amount of experience in python and a little experience in C++ and c#.

I am currently doing an optimization challenge where I am gated by efficiency, and am hoping to use a C library in python to increase efficiency. I have no experience using C in python, but I won't need to marshall many variables. I will need to call a function in python, then from there it can be entirely C.

An example of what I am hoping the code would look like is:

import cLibrary as C

#start python code

def runFunction(string):

#start C

run function in C, have to marshall string

#end C

runFunction(string)

#end python

I am confident with the C/C++ code itself, primary issue is what library/module to use, how to call that library, and how to convert the string from python to C.

  • If you're truly confident with "the C/C++ code" (that's a misnomer, btw) then it should be easy to understand [this](https://docs.python.org/3/extending/extending.html)... What have you tried? – autistic Nov 16 '22 at 23:20
  • You may not even need to write your algorithm in C. You may just need a better algorithm! – SafelyFast Nov 17 '22 at 03:46

2 Answers2

0

In general, you can't write your C code inline within your Python file. Instead, you need to create a separate C program which can get compiled into a library which defines a function that can be imported by Python.

This article appears to have reasonable instructions.

Miguel Guthridge
  • 1,444
  • 10
  • 27
0

Better way to do this will be create a ".dll" (for window) and ".so" file (for Linux) and invoke c code with help of those file.