# Use cc.export function as c/c++ function

**URL:** <https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601>\
**Category:** Support: How do I do ...?\
**Created:** [June 19, 2024, 10:25am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601 "2024-06-19T10:25:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://numba.discourse.group/u/Jonathan)\
**Post date:** [June 19, 2024, 10:25am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/1 "2024-06-19T10:25:15Z")

</div>

Hello,

Is it possible to export a python function with cc.export to create a module library (.so) and then call the python function from c or c++ by linking against the module library ?  
I succeeded in doing it by using pybind, but I would like to know if there is a way to directly call the exported function as a c function.

My goal is to embed python functions in c/c++ library or executable.

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [June 19, 2024, 9:45pm UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/2 "2024-06-19T21:45:45Z")

</div>

It’s not possible as far as I know… You still need a Python interpreter.

You mentioned you did it already with pybind. I didn’t quite understand what you did, but it is possible to get the c function pointers from numba functions that are compiled in a pybind interpreter and then call them directly from the c++ process outside the interpreter.

I use this expensively to get effectively native c++ speed from numba compiled python. (We internally call it native performance embedded python)

---

<div class="post-metadata">

**Author:** ![Kane](https://avatars.discourse-cdn.com/v4/letter/k/76d3ee/32.png) [@Kane](https://numba.discourse.group/u/Kane)\
**Post date:** [August 7, 2024, 6:15am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/3 "2024-08-07T06:15:40Z")

</div>

The idea you provided is really inspiring! However, I still have some questions. Using pybind interpreter means that the numba functions are compiled during running. I wonder if there any methods that I can compile them AOT(ahead of time), and directly get the c function ptr during runtime?

Bsides，could you explain detaily about how to get the c function pointers?

---

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://numba.discourse.group/u/Jonathan)\
**Post date:** [August 7, 2024, 10:10am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/4 "2024-08-07T10:10:08Z")

</div>

Hi Kane,

I finally didn’t use numba.  
I used cython to transform python to c++ and build c++.  
I created a pyx file to wrap a python function and be able to call it from c++.

---

<div class="post-metadata">

**Author:** ![Kane](https://avatars.discourse-cdn.com/v4/letter/k/76d3ee/32.png) [@Kane](https://numba.discourse.group/u/Kane)\
**Post date:** [August 7, 2024, 10:52am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/5 "2024-08-07T10:52:36Z")

</div>

The reason I am trying to use numba is to getting arround GIL when calling the python function in a multithreading way. However I failed. Even though I get the C funtion pointer of numba cfunc, it still hold GIL during calling.

If you have any idea, I would really appreciate it

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [August 7, 2024, 11:14am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/6 "2024-08-07T11:14:34Z")

</div>

Numba-compiled functions don’t release the GIL unless you pass the `nogil=True` kwarg to the decorator. I’m not sure if this kwarg works with `@cc.export` though - can you try it and see if you observe a difference?

---

<div class="post-metadata">

**Author:** ![Kane](https://avatars.discourse-cdn.com/v4/letter/k/76d3ee/32.png) [@Kane](https://numba.discourse.group/u/Kane)\
**Post date:** [August 8, 2024, 2:10am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/7 "2024-08-08T02:10:10Z")

</div>

“nogil” is not avaible in the @cc.export. However, with @jit(nogil=True) decorating the function, it is can be called parallel in python.

My goal is to run the numba compiled function parallel in Cpp threads with GIL released. Would you give me some advice or hints~?

---

<div class="post-metadata">

**Author:** ![gmarkall](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/gmarkall/32/42_2.png) [@gmarkall](https://numba.discourse.group/u/gmarkall)\
**Post date:** [August 8, 2024, 8:35am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/8 "2024-08-08T08:35:14Z")

</div>

I can’t think of a straightforward solution, but perhaps you could use CPython’s C API to release the GIL from your C++ code prior to calling your Numba-compiled code in parallel? Numba does this with calls to [`PyEval_SaveThread()`](https://docs.python.org/3/c-api/init.html#c.PyEval_SaveThread) and [`PyEval_RestoreThread()`](https://docs.python.org/3/c-api/init.html#c.PyEval_RestoreThread) when `nogil=True`.

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [August 8, 2024, 4:12pm UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/9 "2024-08-08T16:12:10Z")

</div>

For getting the cfunc address I started [here](https://stackoverflow.com/questions/32221418/is-it-possible-to-call-numba-jited-function-from-c-c)

---

<div class="post-metadata">

**Author:** ![Kane](https://avatars.discourse-cdn.com/v4/letter/k/76d3ee/32.png) [@Kane](https://numba.discourse.group/u/Kane)\
**Post date:** [August 9, 2024, 7:38am UTC](https://numba.discourse.group/t/use-cc-export-function-as-c-c-function/2601/10 "2024-08-09T07:38:53Z")

</div>

Really appreciate it! I thought my problems are solved.
