Skip to content

Commit 6c00eb9

Browse files
committed
Adds clang missing Init function solution.
1 parent 166a9b7 commit 6c00eb9

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

doc/sphinx/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This describes reliable patterns of coding Python Extensions in C. It covers the
2222
compiler_flags
2323
debugging/debug
2424
thread_safety
25+
miscellaneous
2526

2627

2728
Indices and tables

doc/sphinx/source/miscellaneous.rst

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. highlight:: c
2+
:linenothreshold: 10
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
====================================
8+
Miscellaneous
9+
====================================
10+
11+
------------------------------------
12+
No ``PyInit`` Function Found
13+
------------------------------------
14+
15+
This is probably Mac OS X and Clang specific but when you import your extension and you get an error like:
16+
17+
``ImportError: dynamic module does not define module export function (PyInit_Foo)``
18+
19+
Have a look at the binary.
20+
21+
.. code-block:: sh
22+
23+
$ nm -m Foo.cpython-36m-darwin.so | grep Init
24+
00000000000010d0 (__TEXT,__text) non-external (was a private external) _PyInit_Foo
25+
26+
Sometimes (why?) clang does not make the symbol external. I have found that adding ``__attribute__((visibility("default")))`` to the module initialisation function can fix this:
27+
28+
.. code-block:: sh
29+
30+
__attribute__((visibility("default")))
31+
PyMODINIT_FUNC
32+
PyInit_Foo(void) {
33+
/* ... */
34+
}
35+
36+
And the binary now looks like this:
37+
38+
.. code-block:: sh
39+
40+
$ nm -m cXmlWrite.cpython-36m-darwin.so | grep Init
41+
00000000000010d0 (__TEXT,__text) external _PyInit_cXmlWrite

0 commit comments

Comments
 (0)