|
From: Matthias M. <mm...@ma...> - 2025-12-02 12:10:33
|
Static class members, the way Fl used to be, and namespace members, as it is now, are almmost the same thing in C++. They are global variables and global functions that have an additional name component, Fl::. IIRC you are using pybind, which support C++11, so it should support namespaces as well. I found a few issue reports on this, https://github.com/pybind/pybind11/issues/968 from 2017 for example, so I assume that this is a known factor, and pybind offers a solution. There are no "unnamed namespaces", so everything should be straight forward. If you like, I can take a deeper dive into pybind and try to find out what commands they need to generate the bindings. Here is ChatGPTs opinion with the usual cautions about AI: pybind11 does recognize C++ namespaces — but only if you tell it. It does not discover namespaces automatically. You need to explicitly create the module hierarchy that corresponds to your C++ namespace hierarchy. Here is the rule: ✅ pybind11 mirrors C++ namespaces only when you create submodules manually📌 Example: C++ namespace you want to exposenamespace fltk { int version = 10403; void beep() { /* ... */ } namespace detail { void log(const char* msg) { /* ... */ } } } 📌 pybind11 bindings — how to expose namespaces#include <pybind11/pybind11.h> namespace py = pybind11; PYBIND11_MODULE(fltk, m) { m.attr("version") = fltk::version; m.def("beep", &fltk::beep); // Create a submodule that mirrors namespace fltk::detail py::module_ detail = m.def_submodule("detail", "FLTK internal utilities"); detail.def("log", &fltk::detail::log); } 🔍 What this produces in Pythonimport fltk print(fltk.version) fltk.beep() fltk.detail.log("hello") ❗ Important: pybind11 will NOT detect namespaces for you • It does not parse C++ headers • It does not traverse namespaces • It does not autogenerate submodules You must create the module/submodule mirror yourself.🧩 How pybind11 maps to namespaces C++ construct pybind11 equivalentnamespace X py::module_ X = m.def_submodule("X") free function m.def("name", &func) variable m.attr("var") = value class py::class_<T>(m, "T") nested namespace sub.def_submodule("name")📦 If you want automatic namespace discovery You can use: ✔ pybind11 + Binder (LLVM-based autowrapper) https://github.com/RosettaCommons/binder Binder parses your C++ AST using Clang and generates pybind11 code, including: • namespaces • classes • functions • enums Example: binder --root-module mylib --prefix binder_output.cpp headers.hpp Now you get full namespace reflection without writing manual glue code.🎯 Summary To make pybind11 recognize namespaces: • Create submodules using def_submodule() • Put bindings into those submodules • Repeat for nested namespaces If you want automation, use Binder or Clang-based tooling; pybind11 alone won't do it.If you want, I can generate a pybind11 namespace wrapper template for your FLTK project. |