You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-6Lines changed: 27 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,15 +35,36 @@ I'm a big fan of the workflow that this enables, where you can edit both C++ fil
35
35
36
36
# What's actually going on?
37
37
38
-
**The technical description:** cppimport looks for a C or C++ source file that matches the requested module. If such a file exists, the file is compiled as a Python extension using the options in a Mako header. The extension (shared library) that is produced is placed in the same folder as the C++ source file. Then, the extension is loaded.
38
+
**The technical description:** cppimport looks for a C or C++ source file that matches the requested module. If such a file exists, the file is first run through the Mako templating system. The compilation options produced by the Mako pass are then use to compile the file as a Python extension. The extension (shared library) that is produced is placed in the same folder as the C++ source file. Then, the extension is loaded.
39
39
40
-
**Simpler language please:** Sometimes Python just isn't fast enough. Or you have existing code in a C++ library. So, you write a Python *extension module*, a library of compiled code. I recommend [pybind11](https://github.com/pybind/pybind11) for C++ to Python bindings or [cffi](https://cffi.readthedocs.io/en/latest/) for C to Python bindings. I've done this a lot over the years. But, I discovered that my productivity goes through the floor when my development process goes from *Edit -> Test* in just Python to *Edit -> Compile -> Test* in Python plus C++. So, `cppimport` combines the process of compiling and importing an extension in Python so that you can type `modulename = cppimport.imp("modulename")` and not have to worry about multiple steps. Internally, when no matching Python module is found, `cppimport` looks for a file `modulename.cpp`. If one is found, it's compiled and loaded as an extension module.
40
+
**Simpler language please:** Sometimes Python just isn't fast enough. Or you have existing code in a C++ library. So, you write a Python *extension module*, a library of compiled code. I recommend [pybind11](https://github.com/pybind/pybind11) for C++ to Python bindings or [cffi](https://cffi.readthedocs.io/en/latest/) for C to Python bindings. I've done this a lot over the years. But, I discovered that my productivity goes through the floor when my development process goes from *Edit -> Test* in just Python to *Edit -> Compile -> Test* in Python plus C++. So, `cppimport` combines the process of compiling and importing an extension in Python so that you can type `modulename = cppimport.imp("modulename")` and not have to worry about multiple steps. Internally, `cppimport` looks for a file `modulename.cpp`. If one is found, it's run through the Mako templating system to gather compiler options, then it's compiled and loaded as an extension module.
41
41
42
-
# Notes
43
-
[1]: The compilation should only happen the first time the module is imported. The C++ source is compared with a checksum on each import to determine if the file has changed. Included files are also incorporated into the checksum so recompilation happens automatically when a header file is edited.
42
+
### Recompilation only happens when necessary:
43
+
Compilation should only happen the first time the module is imported. The C++ source is compared with a checksum on each import to determine if the file has changed. Additional dependencies (header files!) can be tracked by adding to the Mako header:
44
+
```
45
+
cfg['dependencies'] = ['file1.h', 'file2.h']
46
+
```
47
+
48
+
### I need to set the compiler or linker args!
49
+
```
50
+
cfg['linker_args'] = ['...']
51
+
cfg['compiler_args'] = ['...']
52
+
cfg['libraries'] = ['...']
53
+
cfg['include_dirs'] = ['...']
54
+
```
55
+
56
+
### I want multiple source files for one extension!
57
+
```
58
+
cfg['sources'] = ['...']
59
+
```
60
+
61
+
### I need more output!
62
+
Calling `cppimport.set_quiet(False)` will result in output that will be helpful in debugging compile errors.
44
63
45
-
[2]: Calling `cppimport.set_quiet(False)` will result in output that will be helpful in debugging compile errors. The default is to make the import process completely silent.
64
+
### Sometimes I need to force a rebuild even when the checksum matches
65
+
Call `cppimport.force_rebuild()` before running `cppimport.imp(...)`.
46
66
47
-
[3]: cppimport currently does not work on Windows. If you're on Windows and you really want cppimport, I'll happily accept a pull request.
67
+
### Windows?
68
+
I don't know if `cppimport` works on Windows. If you're on Windows, try it out and I'll happily accept a pull request for any issues that you fix.
0 commit comments