Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to access the properties or methods of C++ objects created with LibraryLink or MathLink like with J/Link ? Such capability doesn't seem built-in yet and I'm not sure it ever will as Java has introspection capabilities (the ability to retrieve information about classes' members) that I'm not sure C++ has. But maybe I'm wrong ?

share|improve this question
Both with MathLink and LibraryLink you need to write your code in a special way to be able to interface with Mathematica, so any reflection capability is not needed. You can't just "load a class" and use it, as with JLink. You need to write the C (not C++!) interface between e.g. LibraryLink and the external library you're using. If you're using a precompiled shared library (e.g. DLL on Windows), then depending on the technology that library uses (e.g. COM) there may be other ways to load it and enumerate methods. – Szabolcs Jun 26 '12 at 14:48
See for example here. So, generally this is not possible with C++ as much of the information about classes may be lost during compilation. But there are certain technologies which provide some level of reflection. It all depends on what library you want to use, and how. – Szabolcs Jun 26 '12 at 14:49
up vote 8 down vote accepted

No, it is not possible, without creating a custom C++ compiler with enhanced run-time which would support reflection, because C++ does not have a reflection API (JLink capability of accessing properties or methods is based on Java Reflection). There are some libraries / attempts to provide reflection-like functionality to C++, but all of them are (AFAIK) fundamentally limited (e.g. compile-time reflection) and incomplete (I am not even sure that full-fledged and reliable run-time reflection can be achieved without automatic garbage collection, even in principle - and C++ is not a garbage-collected language).

You may have better luck with the D language, which seems to have some reflection capabilities (I don't have experience with it). AFAIK, D can transparently interoperate with C/C++, in particular consume any C++ library without extra wrappers. But this is of course not the same as run-time reflection.

As another, and IMO, very interesting option, you can embed JVM into your application via JNI (Java Native Interface), in which case, you should be able to use Java from LibraryLink, without much performance overhead, since the JVM will then be the same process with the kernel. This option, of course, won't help you if you need to use existing C++ libraries (although, many of them have been ported to Java, so it may make sense to check that). Also, this option would still require custom code.

Apart from these technical obstacles, note alse that JLink does much more than MathLink or LibraryLink: the existence of reflection allows it to know which methods / fields a given class has, at run-time. So, what it does, when you call LoadJavaClass, is to generate lots of boilerplate Mathematica code for fields and methods, and it is this boilerplate code already in place which allows us to call Java methods and access fields in Mathematica with such ease. You would need some similar code-generation module to reach the same degree of simplicity for accessing C++ code, even if C++ had the reflection API.

share|improve this answer
As a general comment, it may be possible to build such a utility using the LLVM compiler suite. The simplest method may be to parse the intermediate representation (IR) which is fairly standardized. It wouldn't be a trivial task, though. And, unless you keep the IR, or compatible equivalent, then this would not be a run time tool. – rcollyer Jun 26 '12 at 14:44
@rcollyer Thanks, similar ideas crossed my mind. But, I think, this is a hard task, and aiming at C++ does not look future-proof to me. At the risk of being flamed, I'd say that IMO C++ is an overly complex and not very well designed language, and with the advances of modern concurrent garbage collectors and other features of modern JDK (or, probably, .Net), the speed / memory advantage of C++ becomes negligilbe, if at all existing. So, we are then only talking about interfacing with the existing libraries, and that does not seem (to me) to warrant such an effort (given the complexity of C++). – Leonid Shifrin Jun 26 '12 at 14:49
The biggest problem I have with garbage collection is it is non-deterministic from the perspective of the running program. This can play merry havoc with performance intensive codes. That's not to say it hasn't improved in the past several years, but I haven't seen evidence that this doesn't occur any longer. I may be wrong. As to being overly complex, it really depends. However, you are absolutely correct in that it would be an interesting project (maybe with a c extension, too), but likely not worth the time needed to make it work. – rcollyer Jun 26 '12 at 14:54
@rcollyer Besides, modern concurrent programs are non-deterministic in any case, due to concurrency. So, I think that in the modern multi-core world, fine-grained determinism is becoming a myth anyway. But, anything I said about C++ does not hold for C, which I like very much, and which I believe is not going away. – Leonid Shifrin Jun 26 '12 at 15:02
@rcollyer "I had no doubts it had improved, I just wasn't sure how much" - from what I know, tremendously. The whole industry was pushing it for more than a decade, with such giants as Sun, Oracle and IBM being hard-core Java / JVM users, to name a few - plus, of course, Microsoft with competing .Net platform. I agree that a C version would be interesting. – Leonid Shifrin Jun 26 '12 at 15:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.