Commit 855863f2 authored by Johannes Blaschke's avatar Johannes Blaschke

add lib naming link and refactor markdown

parent 737d4b52
.ipynb_checkpoints
......@@ -11,14 +11,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"This is a practical demonstration of how cython can be used to wrap a C++ function. This has already been discussed here: https://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html, as well as https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html.\n",
"This is a practical demonstration of how cython can be used to wrap a C++ function. This has already been discussed [in cython's c++ docs][wrapping_cpp], as well as [the docs on external source files][source_files].\n",
"\n",
"But there is a problem with the methon discussed therein: it requires the user to include the C++ source files (which is not difficult if you're using `distutils` from a `setup.py`), which is currently not supported by the `%cython` magic. A workaround is documented here: https://stackoverflow.com/questions/37426534/how-can-i-import-an-external-c-function-into-an-ipython-notebook-using-cython but I feel this is unsuitable for two reasons:\n",
"But there is a problem with the methon discussed therein: it requires the user to include the C++ source files (which is not difficult if you're using `distutils` from a `setup.py`), which is currently not supported by the `%cython` magic. A workaround is documented [on StackOverflow][jupyter_ext_c] but I feel this is unsuitable for two reasons:\n",
"\n",
"1. It still requires access to the C++ sources (which can be unwieldy for large projects)\n",
"2. It uses jupyter to generate a whole lot of termporary (garbade) files, which can clutter up an applications/results dir\n",
"\n",
"Especially point 1 gives me pause, because I'm looking for a way to interact with parts of a larger project (without having to rebuild half of it). Hence we're going to try something else here..."
"Especially point 1 gives me pause, because I'm looking for a way to interact with parts of a larger project (without having to rebuild half of it). Hence we're going to try something else here...\n",
"\n",
"[wrapping_cpp]: https://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html\n",
"[source_files]: https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html\n",
"[jupyter_ext_c]: https://stackoverflow.com/questions/37426534/how-can-i-import-an-external-c-function-into-an-ipython-notebook-using-cython"
]
},
{
......@@ -51,7 +55,7 @@
"\n",
"### Convert `.o` files to Linkable Libraries\n",
"\n",
"First we convert `addone.o` to `libaddone.so`. **Note** the name change: adding `-lX` to the linker args, makes the linker look for `libX.so` in the linker's search path (specified by the `-L` argument):\n",
"First we convert `addone.o` to `libaddone.so`. **Note** [the name change][linker_naming]: adding `-lX` to the linker args, makes the linker look for `libX.so` in the linker's search path (specified by the `-L` argument):\n",
"\n",
"```bash\n",
"clang++ -fpic -shared -Wl,-all_load addone.o -o libaddone.so\n",
......@@ -68,7 +72,9 @@
"\n",
"libaddone.so: addone.o\n",
"\t$(CC) $(SO) $< -o $@\n",
"```"
"```\n",
"\n",
"[linker_naming]: https://stackoverflow.com/questions/16710047/usr-bin-ld-cannot-find-lnameofthelibrary"
]
},
{
......@@ -105,7 +111,7 @@
"\n",
"#### Use the right `%%cython` magic arguments\n",
"\n",
"We need to tell cython to use C++ mode: `--cplus`, and we need to tell it where to look for libraries and which ones to link. Remember that we've converted our C++ object file into a shared library. In our case, this library lives in the `./main_proj/` directory (relative to this notebook), so the compiler arguments are pretty simple: `-I./main_proj -L./main_proj -laddone`. All of these parameters are documented here: https://ipython.org/ipython-doc/2/config/extensions/cythonmagic.html\n",
"We need to tell cython to use C++ mode: `--cplus`, and we need to tell it where to look for libraries and which ones to link. Remember that we've converted our C++ object file into a shared library. In our case, this library lives in the `./main_proj/` directory (relative to this notebook), so the compiler arguments are pretty simple: `-I./main_proj -L./main_proj -laddone`. All of these parameters are documented [in the cython magic docs][cythonmagic_docs].\n",
"\n",
"\n",
"### Dyncamic Linkers Beware\n",
......@@ -118,7 +124,8 @@
"\n",
"And we're done!\n",
"\n",
"[1]: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/UsingDynamicLibraries.html"
"[1]: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/UsingDynamicLibraries.html\n",
"[cythonmagic_docs]: https://ipython.org/ipython-doc/2/config/extensions/cythonmagic.html"
]
},
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment