This is a demo over how to use Tolc the bindings compiler. The goal is to make it trivial to use C++ from other languages.
This repo contains the project MyCppLib that exposes just a simple function Hello::cppFunction that we want to use from python, Objective-C and javascript.
In the CMakeLists.txt we download the latest version of Tolc and uses the tolc_create_bindings function provided by the Tolc package. When the bindings are built, the deliverables are copied to the corresponding directory (./python, ./objc, and ./wasm). In order to know what to copy we pass -Dlanguage=python at configure time.
You need the following installed locally:
CMake(version>=3.15)python3- For
pythonbindings:pythonheaders (python3-devon Debian/Ubuntu, usually included elsewhere)
- For
WebAssemblybindings:node- To run theWebAssemblyexample
- For
Objective-Cbindings:MacOS- Depends on theFoundationlibrary provided byMacOS
Configure the build files:
$ cmake -S. -Bbuild-py -Dlanguage=pythonBuild the module:
$ cmake --build build-pySince this copies the built artifacts (e.g. MyCppLib.cpython-39-darwin.so) to ./python we can now run:
$ python3 python/main.pyNote that this only works on MacOS.
Configure the build files:
$ cmake -S. -Bbuild-objc -Dlanguage=objcBuild the module:
$ cmake --build build-objcYou can now find the built Objective-C library with the corresponding header in the objc directory.
Run the example code (./objc/main.m:
$ ./build-objc/mainWe'll start by downloading and configuring Emscripten. Their SDK is stored as a submodule in this repository so all we have to do is:
$ git submodule update --init
$ cd emsdk
# Download Emscripten to emsdk/upstream
$ ./emsdk install 3.1.3
# Write the .emscripten config file
$ ./emsdk activate 3.1.3Or any later version of emsdk.
Note that for Windows you switch out ./emsdk for emsdk.bat. Now we can build the actual project.
Configure the build files:
$ cmake -S. -Bbuild-wasm -Dlanguage=wasm -DCMAKE_TOOLCHAIN_FILE=emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmakeNote that the / is used as a directory separator even on Windows.
Build the module:
$ cmake --build build-wasm
$ node wasm/main.jsSince this copies the built artifacts (MyCppLib.js and MyCppLib.wasm) to ./wasm we can now run:
$ node wasm/main.jsYou can read more over at the documentation for Tolc.