Overview
| module url | N/A | ||||||||||||||||||||||||||||||
| git repository | https://bitbucket.org/arrizza-public/swig-cpp | ||||||||||||||||||||||||||||||
| git command | git clone git@bitbucket.org:arrizza-public/swig-cpp.git | ||||||||||||||||||||||||||||||
| verification report | https://arrizza.com/web-ver/swig-c++-report.html | ||||||||||||||||||||||||||||||
| version info |
|
- copyright: Copyright and License
- repo status: Repo Information
- installation: Common Setup
Summary
A discussion and project for using SWIG C++ for Python, Ruby and javascript functions.
I had a need to call the same C++ functions from multiple scripting languages: python, ruby, javascript. This project is an example of how to set that up.
Other good sources for SWIG & python:
Next Steps
For SWIG documentation, see Swig 4.3 doc
$ swig -version
SWIG Version 4.0.1
Compiled with g++ [x86_64-pc-linux-gnu]
Configured options: +pcre
Please see http://www.swig.org for reporting bugs and further information
- current calls only use simple functions. Need to try on various other data structures:
- strings
- arrays
- structs
- unions
- JSON
- enums (i.e. read-only)
- pointers (is this needed?)
Common
- The source code is in src directory. The example.cpp and .h come from (with minor changes) SWIG example
example.iis used across all sub-targets and is used by swig to configure what is made available to the language- I used
#include "../src/example.h"and%include. The assumption is that all and only exported functions and variables are named in the example.h file. This may not be true for all projects or all scripting languages. - the functions in example.c are simple. No unions, no structs, no char* or kinds of pointers, no arrays, i.e. simple. Other C constructs may cause some additional definitions to be needed.
- I used
Do a clean rebuild and test
zpm clean- wipes out all the output directories and the cmake build.zpm install full- insures latest ruby, python, node are installedzpm mk-gen- generates Makefile for the current OSzpm mk-build- builds using makezpm doit- runs scripts for all languages
Overall:
# expected stdout:
zpm doit
==== versions
python: Python 3.12.3
ruby : ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [x86_64-linux]
node : v24.16.0
==== run
: python ruby node
fact : 720 720 720
my_mod : 3 3 3
before : 3.0 3.0 3.0
after : 5.1 5.1 5.1
sum1 : 261 261 261
sum2 : 261 261 261
append : abc-ok abc-ok abc-ok
app before : 3.0 3.0 3.0
app after : 5.2 5.2 5.2
app append : xy-app xy-app xy-app
==== convert to hex
python : 01 02 03 FF 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
ruby : 01 02 03 FF 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
node : 01 02 03 FF 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10
- fact - factorial, passes in an integer, returns an integer
- my_mod - modulus, passes in two integers, returns an integer
- before - gets the default value of global variable my_variable
- after - sets my_variable and then gets it again
- sum1 - sums an array of integers
- sum2 - sums an array of bytes (uint8_t)
- append - concats "-ok" to given string
- app before - App::my_variable default value
- app after - App::my_variable is set then gets it again
- app append - App.append() call, concats "-app" to given string
- second section shows to_hex() output
Run test.py
Output directory: out/py
The import loads the factorial function, the mod function and cvar. "cvar" is used to access global C variables. Note these are read/write.
from out.py.example import fact, my_mod, cvar
Expected output:
$ python3.12 test.py
# see doit output above, column starting with "python"
Run test.rb
Output directory: out/rb
The require is very simple in ruby, it loads the .so:
require_relative './out/rb/example.so'
Expected output:
$ ruby test.rb
# see ./doit output above, column starting with "ruby"
Run test.js
For good examples of calls in node_wrap.h, see Node Addon For node_api.h doc & usage: Node n-api
Output directory: out/js
The require is very simple in javascript, it loads the .node:
const example = require("./out/js/example.node")
Expected output:
$ node test.js
# see ./doit output above, column starting with "javascript"