|
From: Andreas H. <and...@bl...> - 2025-12-02 20:08:17
|
Hi Matthias Thanks for pointing out the added namespaces. I actually created a branch of pyFltk for covering fltk1.5 some time ago, but it seems that the namespaces are quite a recent addition and I didn't have a look at t yet. Actually, we are not using pybind but swig to wrap fltk. At the moment, swig is still namespace unaware, that means, everything is merged into the global module namespace. It seems there might b a possibility by creating separate modules bu this is something I still need to get better acquainted with. If you have some good ideas how to best cover this then you are more than welcome. Best regards Andreas On 12/2/25 12:55, Matthias Melcher wrote: > 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. > > _______________________________________________ > Pyfltk-user mailing list > Pyf...@li... > https://lists.sourceforge.net/lists/listinfo/pyfltk-user |