Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
9be1a527
Commit
9be1a527
authored
Jan 20, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrote the initial tutorial on calling into C libraries
parent
2a053274
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
0 deletions
+80
-0
src/tutorial/external.rst
src/tutorial/external.rst
+79
-0
src/tutorial/index.rst
src/tutorial/index.rst
+1
-0
No files found.
src/tutorial/external.rst
0 → 100644
View file @
9be1a527
Calling C functions
====================
This tutorial describes shortly what you need to know in order to call
C library functions from Cython code. For a longer and more
comprehensive tutorial about using external C libraries, wrapping them
and handling errors, see :doc:`clibraries`.
For simplicity, let's start with a function from the standard C
library. This does not add any dependencies to your code, and it has
the additional advantage that Cython already defines many such
functions for you. So you can just cimport and use them.
For example, let's say you need a low-level way to parse a number from
a ``char*`` value. You could use the ``atoi()`` function, as defined
by the ``stdlib.h`` header file. This can be done as follows::
from libc.stdlib cimport atoi
cdef parse_charptr_to_py_int(char* s):
assert s is not NULL, "byte string value is NULL"
return atoi(s) # note: atoi() has no error detection!
You can find a complete list of these standard cimport files in
Cython's source package ``Cython/Includes/``. It also has a complete
set of declarations for CPython's C-API. For example, to test at C
compilation time which CPython version your code is being compiled
with, you can do this::
from cpython.version cimport PY_VERSION_HEX
print PY_VERSION_HEX >= 0x030200F0 # Python version >= 3.2 final
Cython also provides declarations for the C math library::
from libc.math cimport sin
cdef double f(double x):
return sin(x*x)
However, this is a library that is not linked by default on Unix-like
systems, such as Linux or MacOS-X. In addition to cimporting the
declarations, you must configure your build system to link against the
shared library ``m``. For distutils, it is enough to add it to the
``libraries`` parameter of the ``Extension()`` setup::
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules=[
Extension("demo",
["demo.pyx"],
libraries=["m"]) # Unix-like specific
]
setup(
name = "Demos",
cmdclass = {"build_ext": build_ext},
ext_modules = ext_modules
)
If you want to access C code for which Cython does not provide a ready
to use declaration, you must declare them yourself. For example, the
above ``sin()`` function is defined as follows::
cdef extern from "math.h":
double sin(double)
This instructs Cython to generate code that imports the ``math.h``
header file and declares the ``sin()`` function in a way that makes it
available to Cython code. The C compiler will see the original
declaration in ``math.h`` at compile time, but Cython does not parse
"math.h" and requires a separate definition.
Just like the ``sin()`` function from the math library, it is possible
to declare and call into any C library as long as the module that
Cython generates is properly linked against the shared or static
library.
src/tutorial/index.rst
View file @
9be1a527
...
...
@@ -4,6 +4,7 @@ Tutorials
.. toctree::
:maxdepth: 2
external
clibraries
cdef_classes
pxd_files
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment