Julia with C++ (C++20 and modules) vs. e.g. Python and pybind11

Just curious, has anyone used/compared the “excellent” pybind11 (that supports at least C++17, not sure with C++20) and Julia with Cxx.jl and/or Cppwrap.jl?

Note, Pybind11 has a benchmark section in their extensive docs.

I’ve not used any of this (yet). I just find the Cxx.jl C++ REPL integration cool (not sure other languages have anything yet as good).

Is there a reason to prefer Python or other languages with C++ over Julia (with C++ or alone…) because of better C++ interop (e.g. for performance or other, e.g. docs reasons)? I know at least C interop is superior in Julia to SWIG. I suppose you can call Python (with PyCall) and then to C++, but not really needed/wanted.

What comes to mind, brand new in C++20, is (finally) modules:

Not sure about support from other languages (or if needed).

There’s no need to explain Cxx.jl vs. Cppwrap.jl (but feel free to point to what best option for Python maps to each, CppWrap maps to Boost; and I think pybind11, didn’t look carefully).

EDIT:
I know both support Julia 1.0 (and 1.2?) and Cxx has had updates since I last looked at it (while still having open Windows issues, unlike CxxWrap, as if I recall it wouldn’t need special support; both should work well on Linux).

I did see this “Move GC protect code to JuliaSee PR” recentlly merged for CppWrap; one potential issue is Julia’s GC (needing support, with Julia evolving; different from Python’s reference counting).

I don’t know anything to be wrong with CxxWrap, seems very nice; neither have as extensive docs or benchmark section as far as I recall. Note, however there’s:

https://juliacomputing.com/blog/2017/12/01/cxx-and-cxxwrap-intro.html

and I also found: https://tianjun.me/static/essay_resources/A_Guide_to_Wrap_a_C++_Library_with_CXXWrap.jl_and_BinaryBuilder.jl_in_Julia/main.html C++Now 2018: The Julia Language and C++: The Perfect...

and there are some recent discussions here, e.g menioning one minor TODO for CppWrap:

I don’t know the details, but I’m sure it’s safe to assume that pybind11 is quite a lot more mature than Cxx.jl. However, I don’t think it would make sense to choose between these two options based on the wrapper itself: Python and Julia are very different languages, so it’s really a matter of what you want to use it for. Python is a scripting language, so if you’re looking at an application where the majority of your run-time really is going to be in C++ binaries, and you need a convenient orchestration tool, Python and pybind can be a fine choice. If however you want to use some C++ libraries, but intend on writing the majority of your program outside of C++, then Julia of course has a huge performance advantage over Python. If you don’t care about performance, I’d imagine much of your decision would be guided by your personal language preference.

Something else that you might need to consider is that Cxx.jl necessarily uses clang to compile C++ code, but if your main interest is to link to C++ shared libs, I don’t think that matters. I would imagine that C++20 support hinges entirely on clang's C++20 support, but this is speculation, so perhaps someone more knowledgeable will chime in about that.

This is the first I’m personally hearing about C++20 modules. That definitely sounds like something I would have been extremely excited about in my C++ days.

5 Likes

Right, I wouldn’t choose another language over Julia, even if interop not as good (Julia’s FFI speed is superior, on par with Lua’s, other languages not close for calling C, not sure for C++; benchmarks for pybind11 where for compiled size and to a lesser degree C++ compile speed vs. Boost; not sure how e.g. CppWrap stacks up). And I doubt it’s better elsewhere (possibly docs? or more locatable, note links I added to my top post).

Some people might however consider this, or just know of the “excellent” pybind11. And often you’re gluing together C++ code, not adding much, and then interop may be the most important factor, and even Python’s speed/interactivity a plus over Julia… or that used to be the case, rapidly a non-issue with good workarounds in Julia (and e.g. new Julia interpreter).

I was more looking for potential Julia C++ interop flaws vs Python’s solutions, not to avoid Julia, but in case there’s a way (or need) for help. I know about the issues on both Github projects. And curious if others have found a reason regarding this to avoid Julia.

I’m pretty sure pybind11 is a lot more mature than CxxWrap, but basically the approach is the same, CxxWrap was inspired by Boost.Python, which also inspired pybind11. C++ support depends entirely on your compiler, CxxWrap itself requires at least a C++14 compiler now, but it should compile on newer standards as far as I’m aware.

I’m currently working on issue 134, which is triggering a major rewrite of some of the CxxWrap internals (work is in the type_overhaul branch).

2 Likes