# Using ROOT in a c++ code

**URL:** <https://root-forum.cern.ch/t/using-root-in-a-c-code/48257>\
**Category:** Newbie\
**Tags:** roofit, hist, root\
**Created:** [January 4, 2022, 11:02am UTC](https://root-forum.cern.ch/t/using-root-in-a-c-code/48257 "2022-01-04T11:02:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavideLP](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/b5e925/32.png) [@DavideLP](https://root-forum.cern.ch/u/DavideLP)\
**Post date:** [January 4, 2022, 11:02am UTC](https://root-forum.cern.ch/t/using-root-in-a-c-code/48257/1 "2022-01-04T11:02:06Z")

</div>

Hi, I’m coding as a project a c++ simulation of ALICE experiment. I need to use the various ROOT classes and features in my code: but I don’t know  
-1) What should I include (and where: only in my main? or every where I use ROOT’s features) in my code (I mean #include “etcetc”)  
-2) How should i compile the code

I have 3 .cpp files and their relative .hpp, and the main.cpp.  
Until now I compiled like this: g++ file1.cpp file2.cpp file3.cpp main.cpp

And I should say I’m not able to compile using CMake, but I would like to learn how to, so if anyone knows some good guide/tutorial it would be very kind if you reccomended it. Thanks

I will use the basic tools of ROOT, but for the #include just go safe, it’s better if there are more than needed than less. (I will try which one are really necessary in a second moment)  
For example i need to use random generator, histograms and 1d functions.

Thanks for the help!

\_ROOT Version: Updated in November 2021  
\_Platform: Ubuntu  
\_Compiler: g++

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 4, 2022, 12:27pm UTC](https://root-forum.cern.ch/t/using-root-in-a-c-code/48257/2 "2022-01-04T12:27:39Z")

</div>

`$(root-config --cxx --cflags) file1.cpp file2.cpp file3.cpp main.cpp $(root-config --libs)`

---

<div class="post-metadata">

**Author:** ![DavideLP](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/b5e925/32.png) [@DavideLP](https://root-forum.cern.ch/u/DavideLP)\
**Post date:** [January 4, 2022, 2:59pm UTC](https://root-forum.cern.ch/t/using-root-in-a-c-code/48257/3 "2022-01-04T14:59:34Z")

</div>

Thanks, that seem to work. I found some indication of the professor, but I encounter various error. I’ll post them here so maybe you could tell me if the commands seem right to you

1. .L file1.cpp+  
.L file2.cpp+  
.L file3.cpp+

2. write, in the main.cpp, R\_\_LOAD\_LIBRARY( ClassName\_cpp.so) after including the headers of file1, file2, and file3 (they contain a class each).

3. .L main.cpp+  
main()

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [January 4, 2022, 4:37pm UTC](https://root-forum.cern.ch/t/using-root-in-a-c-code/48257/4 "2022-01-04T16:37:29Z")

</div>

concerning your cmake question, have a look at [this](https://root.cern/manual/integrate_root_into_my_cmake_project/)

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [January 4, 2022, 4:51pm UTC](https://root-forum.cern.ch/t/using-root-in-a-c-code/48257/5 "2022-01-04T16:51:17Z")

</div>

Not completely sure what you want to accomplish here.  
Please have a look at [this](https://root.cern/primer/) and scroll  
down to the section " _ **Compile a Macro with the Compiler** _"

In part 1, you are creating three libraries, one for each .cpp file.  
In part 2, you want to load these libraries but that has actually already  
happened in part 1.  
In part 3 you do this with the main program, all dependencies on ROOT libraries  
and you functions should have been resolved. You follow this by executing  
the function “main”.

What did not work for you ?
