Skip to content

Instantly share code, notes, and snippets.

@johntyree
Last active May 17, 2018 12:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johntyree/4560948 to your computer and use it in GitHub Desktop.
Save johntyree/4560948 to your computer and use it in GitHub Desktop.
C vs. C++ in Cython.
# coding: utf8
# distutils: language = c++
# distutils: sources = b.cpp
# Using C calling convention works with "cdef extern ..." directly; no
# hpp header.
# Without it we need to use the cdef extern from "b.hpp": ... declaration.
# Implicitly forces C calling convenction?
cdef extern void c()
# This works with C++ just fine.
cdef extern from "b.hpp":
void cpp_good()
# This doesn't. Undefined symbol 'cpp_bad'.
cdef extern void cpp_bad()
c()
cpp_good()
cpp_bad()
// Indicate C calling convention for our C function.
extern "C" { void c(); }
void c() { }
void cpp_good() { }
void cpp_bad() { }
void cpp_good();
// If we include cpp_bad() here, we get an error about redefining it with C
// linkage after having already defined it with C++ linkage.
#!/usr/bin/env python
# coding: utf8
from setuptools import setup
from Cython.Build import cythonize
cython_modules = cythonize('*.pyx')
setup(ext_modules = cython_modules)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment