1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
# mode: run
# tag: cpp
# ticket: 1839
"""
PYTHON setup.py build_ext --inplace
PYTHON -c "from include_as_c_and_cpp import test; test()"
PYTHON -c "from include_from_c_and_cpp import test; test()"
PYTHON -c "from cdefines import test; test()"
"""
######## setup.py ########
from Cython.Build import cythonize
from Cython.Distutils.extension import Extension
from distutils.core import setup
import sys
include_as_c_and_cpp = Extension(
"include_as_c_and_cpp",
["include_as_c_and_cpp.pyx", "include_as_c.cpp", "include_as_cpp.cpp"],
)
include_from_c_and_cpp = Extension(
"include_from_c_and_cpp",
["include_from_c_and_cpp.pyx", "include_from_c.c", "include_from_cpp.cpp"],
)
cdefines = Extension(
"cdefines",
["cdefines.pyx", "cdefines_clean.c", "cdefines_plain.cpp"],
define_macros = [("CYTHON_EXTERN_C", 'extern "C"')],
)
ext_modules = [include_as_c_and_cpp, include_from_c_and_cpp]
if sys.platform != "win32":
# It's very hard to get the command-line macro define to escape properly on Windows,
# so skip it
ext_modules.append(cdefines)
setup(
ext_modules=cythonize(ext_modules),
)
######## include_as_c_and_cpp.pyx ########
# distutils: language = c++
from libcpp cimport vector
cdef public vector.vector[int] get_vector():
return [1,2,3]
cdef extern from "include_as_c_and_cpp_header.h":
cdef size_t size_vector1()
cdef size_t size_vector2()
def test():
assert size_vector1() == 3
assert size_vector2() == 3
######## include_as_c_and_cpp_header.h ########
size_t size_vector1();
size_t size_vector2();
######## include_as_cpp.cpp ########
#include <vector>
#include "include_as_c_and_cpp.h"
size_t size_vector1() {
return get_vector().size();
}
######## include_as_c.cpp ########
#include <vector>
extern "C" {
// #include within `extern "C"` is legal.
// We want to make sure here that Cython C++ functions are flagged as `extern "C++"`.
// Otherwise they would be interpreted with C-linkage if the header is include within a `extern "C"` block.
#include "include_as_c_and_cpp.h"
}
size_t size_vector2() {
return get_vector().size();
}
######## include_from_c_and_cpp.pyx ########
cdef public char get_char():
return 42
cdef extern from "include_from_c_and_cpp_header.h":
cdef int get_int1()
cdef int get_int2()
def test():
assert get_int1() == 42
assert get_int2() == 42
######## include_from_c_and_cpp_header.h ########
int get_int1();
int get_int2();
######## include_from_c.c ########
#include "include_from_c_and_cpp.h"
int get_int1() { return (int)get_char(); }
######## include_from_cpp.cpp ########
extern "C" {
#include "include_from_c_and_cpp.h"
}
extern "C" int get_int2() { return (int)get_char(); }
######## cdefines.pyx ########
# distutils: language = c++
cdef public char get_char():
return 42
cdef extern from "cdefines_header.h":
cdef int get_int1()
cdef int get_int2()
def test():
assert get_int1() == 42
assert get_int2() == 42
######## cdefines_header.h ########
#ifdef __cplusplus
#define cdefines_EXTERN_C extern "C"
#else
#define cdefines_EXTERN_C
#endif
cdefines_EXTERN_C int get_int1();
int get_int2();
######## cdefines_clean.c ########
#undef CYTHON_EXTERN_C
#define CYTHON_EXTERN_C
#include "cdefines.h"
int get_int1() { return (int)get_char(); }
######## cdefines_plain.cpp ########
#include "cdefines.h"
int get_int2() { return (int)get_char(); }
######## cdefines.py ##########
# Dummy module so Windows test works
import sys
assert sys.platform == "win32"
def test():
pass
|