1

Cling sounds like a great way to tinker around with a the API of a large library for learning purposes. Unfortunately, there is no documentation or tutorials on how to even get started with this. I kept running into missing symbols, and having to use C++filt and rgrep over the sources over and over again to figure out what library or header to load, until I gave up.

Is the right strategy to JIT the entire library from the sources, or should you link in a pre-built library?

Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100

2 Answers2

2

One way to make the link between headers and libraries is generating an autoloading map (http://cling.web.cern.ch/cling/doxygen/classcling_1_1Interpreter.html#ad56b40974d204f85e9fc0a9fa9af1660). One can generate it at built time and add a hook in the library during its static initialization. Thus the user would do: .L myLib This in turn would trigger header inclusion. The other way is a bit more trickier. Have a look at https://github.com/vgvassilev/cling/tree/master/test/Autoloading

I hope it helps.

vvassilev
  • 316
  • 2
  • 7
  • Thanks for the response! I was just looking for a lightweight way to tinker around with someone else's library interactively. Adding instrumentation to their build of any kind is probably overkill imho; you're probably better off spending the time with their code and API docs if any exist. – Andrew Wagner Nov 15 '14 at 14:21
  • Unfortunately, this is also an issue with c++, right? If you use a header file in your application you will need to specify the library associated with this header file when linking. There is not an easy way around. A feature that could help (but is not implemented) is cling could scan all the libs in the include path and do nm over and brute-forcely find the symbol. The issue is that a symbol can be found in more than one library. – vvassilev Nov 22 '14 at 12:37
  • In my use case it was an open source library, so I had the sources, along with libraries compiled from them, and example programs that link against them. Perhaps it would be possible to make cling automatically load all of the headers and compiled libraries (or source files if that makes more sense) below the current directory to support this use case? – Andrew Wagner Nov 24 '14 at 16:43
  • Yes, it is possible. We don't have the option for load *.h. We make the header loading as a part of library's static initialization. Probably you can also use that trick. – vvassilev Nov 27 '14 at 13:03
1

Apologies for the super-late reply. You should link a pre-built library!

You can use the -l flag to load a dynamic library into cling:

$ echo 'extern "C" const char* zlibVersion();
 zlibVersion()' | cling -lz

Or use .L lib interactively:

[cling]$ .L libz
[cling]$ #include <zlib.h>
[cling]$ zlibVersion()
(const char * const) "1.2.3.4"
Axel Naumann
  • 176
  • 5
  • When working with `cling::Interpreter` in C++, is there a way to use `.L`? `Interpreter::declare` `::process` `::parse` they all do not recognize that syntax. – DarioP Jun 04 '20 at 16:18
  • @Anyone-reading this helped: https://github.com/jupyter-xeus/xeus-cling/issues/87 – DarioP Jun 04 '20 at 18:58