# Building Cling Interpreter with libxml2 external library

**URL:** <https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616>\
**Category:** Newbie\
**Tags:** python, root\
**Created:** [December 21, 2023, 10:09am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616 "2023-12-21T10:09:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [December 21, 2023, 10:09am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/1 "2023-12-21T10:09:02Z")

</div>

Hello, I am trying to build a custom cling interpreter with libxml2 already loaded using the cling Interpreter source file attached below. I build my cling Interpreter using the command below:

```auto
 g++ -c -o cling.o -Iinclude -I/usr/include/libxml2 cling.cpp  
 g++ -o clingD cling.o -lLLVMSupport -lclangASTMatchers -lclangFrontendTool -lclingUserInterface -lclangARCMigrate -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangCrossTU -lclangIndex -lclangFormat -lclangToolingInclusions -lclangToolingCore -lclingMetaProcessor -lclingInterpreter -lclingUtils -lclangCodeGen -lclangRewriteFrontend -lclangFrontend -lclangDriver -lLLVMOption -lclangSerialization -lLLVMCoverage -lLLVMLTO -lLLVMExtensions -lclangParse -lclangSema -lclangAnalysis -lclangASTMatchers -lclangEdit -lclangAST -lclangRewrite -lclangLex -lclangBasic -lLLVMOrcJIT -lLLVMPasses -lLLVMCoroutines -lLLVMObjCARCOpts -lLLVMExecutionEngine -lLLVMJITLink -lLLVMOrcTargetProcess -lLLVMOrcShared -lLLVMRuntimeDyld -lLLVMNVPTXCodeGen -lLLVMipo -lLLVMFrontendOpenMP -lLLVMIRReader -lLLVMAsmParser -lLLVMInstrumentation -lLLVMLinker -lLLVMVectorize -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMX86CodeGen -lLLVMAsmPrinter -lLLVMDebugInfoDWARF -lLLVMDebugInfoMSF -lLLVMGlobalISel -lLLVMSelectionDAG -lLLVMCodeGen -lLLVMBitWriter -lLLVMScalarOpts -lLLVMAggressiveInstCombine -lLLVMInstCombine -lLLVMTransformUtils -lLLVMTarget -lLLVMAnalysis -lLLVMObject -lLLVMBitReader -lLLVMTextAPI -lLLVMProfileData -lLLVMCFGuard -lLLVMCore -lLLVMRemarks -lLLVMBitstreamReader -lLLVMX86AsmParser -lLLVMMCParser -lLLVMX86Desc -lLLVMX86Disassembler -lLLVMMCDisassembler -lLLVMMC -lLLVMBinaryFormat -lLLVMDebugInfoCodeView -lLLVMX86Info -lLLVMSupport -lrt -ldl -lm -lxml2 /usr/lib64/libz.so /usr/lib64/libtinfo.so -lLLVMDemangle

```

If i try and run the attached script using the compile executable, it fails with the attached error.

```auto
input_line_4:1:10: fatal error: 'libxml/parser.h' file not found
#include <libxml/parser.h>
         ^ ~~~~~~~~~~~~~~~~
input_line_5:1:10: fatal error: 'libxml/tree.h' file not found
#include <libxml/tree.h>

```

Please see attached files  
[script.cxx](https://root-forum.cern.ch/uploads/short-url/6Ll4AbGYysw7l9Wx9LH7U8vWaf1.cxx) (374 Bytes)  
[cling.cpp](https://root-forum.cern.ch/uploads/short-url/mbMF4BNpjxFvJ37MMG6rclPRuKu.cpp) (6.1 KB)

---

<div class="post-metadata">

**Author:** ![mczurylo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mczurylo/32/33269_2.png) [@mczurylo](https://root-forum.cern.ch/u/mczurylo)\
**Post date:** [December 21, 2023, 10:21am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/2 "2023-12-21T10:21:28Z")

</div>

Hi @cw2636,

thank you for your question. I will ask @vvassilev to take a look here.

Cheers,  
Marta

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [December 21, 2023, 10:33am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/3 "2023-12-21T10:33:21Z")

</div>

Maybe the we should use `-I/usr/include/` instead of `-I/usr/include/libxml2`.

---

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [December 21, 2023, 10:47am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/4 "2023-12-21T10:47:40Z")

</div>

It didn’t work.  
In cling if i do this

```auto

******************CLING******************
* Type C++ code and press enter to run it *
* Type .q to exit *
*******************************************
[cling]$ .I /usr/include/libxml2
[cling]$ .L xml2
[clin]$ .x cling_bin/script.cxx
This is a test 6

```

It works without no error.

---

<div class="post-metadata">

**Author:** ![SapphireShade](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sapphireshade/32/35327_2.png) [@SapphireShade](https://root-forum.cern.ch/u/SapphireShade)\
**Post date:** [December 21, 2023, 9:52pm UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/5 "2023-12-21T21:52:28Z")

</div>

Could you please provide a more detailed example of how it should work?

---

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [December 22, 2023, 9:47am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/6 "2023-12-22T09:47:41Z")

</div>

Let us take a basic c++ application below which uses libxml2 library to read an xml file.

```auto
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <iostream>
/**
* example1Func:
* @filename: a filename or an URL
*
* Parse the resource and free the resulting tree
*/
static void
example1Func(const char *filename) {
        xmlDocPtr doc; /* the resulting document tree */

        doc = xmlReadFile(filename, NULL, 0);
        if (doc == NULL) {
                fprintf(stderr, "Failed to parse %s\n", filename);
                return;
        }

        std::cout << "Xml has been read" << std::endl;
        xmlFreeDoc(doc);
}

int main(int argc, char **argv) {
        if (argc != 2)
                return(1);

        /*
        * this initialize the library and check potential ABI mismatches
        * between the version it was compiled for and the actual shared
        * library used.
        */
        LIBXML_TEST_VERSION

                example1Func(argv[1]);

        /*
        * Cleanup function for the XML library.
        */
        xmlCleanupParser();
        /*
        * this is to debug memory for regression tests
        */
        xmlMemoryDump();
        return(0);
}

```

In c++ i can compile this file with the libxml2 library and generate the executable using the following commands:

```auto
 g++ -c -o testxml.o -I/usr/include/libxml2 testxml.cpp
g++ -o xml.exe testxml.o -lxml2

```

Executing this file with a test xml file runs with no error.

```auto
./xml.exe test.xml
Xml has been read

```

I am trying to do something similar to this with cling. I wanted to generate a cling Interpreter executable with some shared or external libraries embedded into it. This interprete when used to interprete c++ code, will know where to find the source files of those libraries and call appropriate functions.

---

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [December 22, 2023, 9:49am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/8 "2023-12-22T09:49:36Z")

</div>

@SapphireShade Please refer to this also

---

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [December 22, 2023, 10:17am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/9 "2023-12-22T10:17:59Z")

</div>

> [@cw2636](#):
>
> ```auto
> [cling]$ .I /usr/include/libxml2
> 
> ```

@vvassilev Why if i add these line of code to the cling interpreter source file

```auto
Interp.AddIncludePath("/usr/include/libxml2");
Interp.loadFile("/lib64/libxml2.so");

```

It works, but if we compile and link it on command line wont work?

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [December 22, 2023, 10:42am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/10 "2023-12-22T10:42:42Z")

</div>

> [@cw2636](#):
>
> `g++ -c -o cling.o -Iinclude -I/usr/include/libxml2 cling.cpp`

I misread the original command. You compile the cling with gcc and you pass an include path to gcc. Now cling is a compiler that lives in your binary and you should pass the include path to it. That is why this works.

> [@cw2636](#):
>
> ```auto
> Interp.AddIncludePath("/usr/include/libxml2");
> Interp.loadFile("/lib64/libxml2.so");
> 
> ```
> 
> It works, but if we compile and link it on command line wont work?

if you add `#pragma cling add_include_path("/usr/include/libxml2")` before you include the `libxml` headers it might work.

Probably it’d be more helpful if you describe the problem you are trying to solve first.

---

<div class="post-metadata">

**Author:** ![cw2636](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/a9adbd/32.png) [@cw2636](https://root-forum.cern.ch/u/cw2636)\
**Post date:** [December 22, 2023, 11:01am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/11 "2023-12-22T11:01:25Z")

</div>

Let’s say i want to omit using

```auto
Interp.AddIncludePath("/usr/include/libxml2");
Interp.loadFile("/lib64/libxml2.so");

```

to link and compile libxml2 with cling. What is the best approach. Please specify with code?

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [December 22, 2023, 11:27am UTC](https://root-forum.cern.ch/t/building-cling-interpreter-with-libxml2-external-library/57616/12 "2023-12-22T11:27:57Z")

</div>

> [@cw2636](#):
>
> Let’s say i want to omit using
> 
> ```auto
> Interp.AddIncludePath("/usr/include/libxml2");
> Interp.loadFile("/lib64/libxml2.so");
> 
> ```
> 
> to link and compile libxml2 with cling. What is the best approach. Please specify with code?

I did: `#pragma cling add_include_path("/usr/include/libxml2")`
