# Compile and run from command prompt

**URL:** <https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799>\
**Category:** ROOT\
**Created:** [February 16, 2012, 1:42pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799 "2012-02-16T13:42:05Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alkass](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alkass](https://root-forum.cern.ch/u/Alkass)\
**Post date:** [February 16, 2012, 1:42pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/1 "2012-02-16T13:42:05Z")

</div>

Hi

I apologize if my question is stupid, but here it is 😉

After making the class from the root file, I have the .C and .h files – Is there any way to compile and run “Loop()” from command block rather than everytime having to

[quote]root -l macro.C  
macro m  
m.Loop();  
[/quote]

Thanks

Alex

---

<div class="post-metadata">

**Author:** ![Pepe\_Le\_Pew](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pepe_le_pew/32/12365_2.png) [@Pepe\_Le\_Pew](https://root-forum.cern.ch/u/Pepe_Le_Pew)\
**Post date:** [February 16, 2012, 3:19pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/2 "2012-02-16T15:19:16Z")

</div>

A brutal fix …

```auto
echo 'gROOT->LoadMacro("macro.C"); macro m; m.Loop(); gSystem->Exit(0);' | root -b -l

```

… or …

```auto
root -b -l <<EOF
.L macro.C
macro m;
m.Loop();
.q
EOF

```

---

<div class="post-metadata">

**Author:** ![Alkass](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c67d28/32.png) [@Alkass](https://root-forum.cern.ch/u/Alkass)\
**Post date:** [February 16, 2012, 3:52pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/3 "2012-02-16T15:52:22Z")

</div>

thanks! works!  
-a

---

<div class="post-metadata">

**Author:** ![bmesserl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@bmesserl](https://root-forum.cern.ch/u/bmesserl)\
**Post date:** [June 5, 2013, 7:48pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/4 "2013-06-05T19:48:49Z")

</div>

> [@Pepe Le Pew](#):
>
> A brutal fix …  
> CODE: SELECT ALL  
> echo ‘gROOT-\>LoadMacro(“macro.C”); macro m; m.Loop(); gSystem-\>Exit(0);’ | root -b -l
> 
> … or …
> 
> CODE: SELECT ALL  
> root -b -l \<\<EOF  
> .L macro.C  
> macro m;  
> m.Loop();  
> .q  
> EOF

Both of these seem to work for me, but then the root session closes immediately after. Even when I remove “gSystem-\>Exit(0)” and “.q” from these lines, the macro runs and then root quits. Why is this? Is there any way to leave the root session open after the macro runs? (my .C file generates some plots on a canvas and they close along with the root session). Sorry, I’m still learning bash/root/C++.

---

<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:** [June 5, 2013, 8:22pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/5 "2013-06-05T20:22:06Z")

</div>

[ROOT Primer -\> 3 ROOT Macros](https://root.cern.ch/root/htmldoc/guides/primer/ROOTPrimer.html#root-macros)  
[ROOT User’s Guide -\> Getting Started -\> Start and Quit a ROOT Session](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#start-and-quit-a-root-session)

---

<div class="post-metadata">

**Author:** ![bmesserl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@bmesserl](https://root-forum.cern.ch/u/bmesserl)\
**Post date:** [June 5, 2013, 9:34pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/6 "2013-06-05T21:34:59Z")

</div>

Thanks for the quick response. As helpful as these documents are, I don’t think they contain the answer to my problem. As the original poster described, I’ve made a class from a root file, I have .C and .h files, and so in order to compile and run “Loop()” I need three lines:

`root -l macro.C
macro m
m.Loop();`

The methods described in the documentation don’t (explicitly anyway) tell you how to deal with macros which don’t have the same name as the main function. For example, the first lines of my macro.c file look like [code]#define macro\_cxx  
#include “macro.h”

void macro::Loop()  
{  
…  
}[/code]

The documentation seems to imply that I can conclude the macro.c file with something like:

`#ifndef __CINT__
 int main(){
     Loop();
     return 0;
     }
 #endif`in order to make it a standalone application, but I’ve had no luck with that either. And besides that seems like overkill, when all I want to do is reduce 3 or four command prompt lines to 1. It seems like it should be a simple fix from pepe’s first or second suggestions.

---

<div class="post-metadata">

**Author:** ![zatserkl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/f1d935/32.png) [@zatserkl](https://root-forum.cern.ch/u/zatserkl)\
**Post date:** [June 5, 2013, 9:54pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/7 "2013-06-05T21:54:05Z")

</div>

This is working example. See comments how to compile (file name root.cpp, standard executable will have name a.out).

/\*  
NB: inverse quotes ``!  
g++ `$ROOTSYS/bin/root-config --cflags --glibs` root.cpp

To build dictionaries see, e.g. xmltree.cpp. I compiled it using

rootcint -f xmltree\_dict.cxx -c xmltree.h xmltree\_linkdef.h  
g++ `$ROOTSYS/bin/root-config --cflags --glibs` -o xmltree xmltree.cpp xmltree\_dict.cxx

See details about dictionaries in ROOT User Guide,  
chapter “Writing a Graphical User Interface”, part “A Standalone Example”  
\*/

#include \<TRint.h\> // the only include you need  
//#include \<TROOT.h\> // uncomment to use global variables like gPad, etc.

#include   
#include

#include \<TH1.h\>  
#include \<TCanvas.h\>

using std::cout; using std::endl;

main(int argc, char _argv[])  
{  
TRint_ theApp = new TRint(“Rint”, &argc, argv, 0, 0, 1); // do not show splash screen

// code starts here

TH1F\* h = new TH1F(“h”, “h”, 100, -3, 3);

h-\>FillRandom(“gaus”, 10000);

cout\<\< "h-\>GetMean() = " \<\< h-\>GetMean() \<\< " h-\>GetRMS() = " \<\< h-\>GetRMS() \<\<endl;

new TCanvas();  
h-\>Draw();

// work in command line mode  
theApp-\>Run();  
delete theApp;  
}

---

<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:** [June 5, 2013, 9:59pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/8 "2013-06-05T21:59:44Z")

</div>

Try to create and use a simple “RunMe.cxx” file:

```auto
#include "macro.C"

//
// "ROOT Script" entry point (the same name as the "filename's base").
//
// [bash/csh] root RunMe.cxx
// [bash/csh] root RunMe.cxx++
// root [0] .x RunMe.cxx
// root [0] .x RunMe.cxx++
//
void RunMe(void)
{
  macro m;
  m.Loop();
}

#if !defined( __CINT__ ) && !defined( __ACLIC__ )
//
// "Standalone Application" entry point ("main").
//
// `root-config --cxx --cflags` -o RunMe RunMe.cxx `root-config --libs`
// ./RunMe
//
int main(int /*argc*/, char ** /*argv*/)
{
  RunMe(); // just call the "ROOT Script"
  return 0;
}
#endif /* !defined( __CINT__ ) && !defined( __ACLIC__ ) */

```

---

<div class="post-metadata">

**Author:** ![bmesserl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@bmesserl](https://root-forum.cern.ch/u/bmesserl)\
**Post date:** [June 5, 2013, 11:06pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/9 "2013-06-05T23:06:27Z")

</div>

Thank you Wile! I got it working with your script.

Now I’ll just try to understand it 🙂

zatserkl - I fooled around with yours for a while, but I had no luck. Thanks though.

---

<div class="post-metadata">

**Author:** ![bmesserl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@bmesserl](https://root-forum.cern.ch/u/bmesserl)\
**Post date:** [June 12, 2013, 5:08pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/10 "2013-06-12T17:08:15Z")

</div>

I just want to follow up here, for the record, and in case anyone is following.

The command that I was looking for is:`gROOT->ProcessLine("line of root command");`

A macro which would normally run with the initialization of ROOT plus three separate root commands:

```auto
$ root -l
root [0] .L Macro.C
root [1] Macro t
root [2] t.Loop()
[/code] can be reduced to a single line:
[code]$ root -l runme.C[/code]

Where runme.C contains exactly:
[code]void runme(){
    gROOT->ProcessLine(".L Macro.C");
    gROOT->ProcessLine("Macro t");
    gROOT->ProcessLine("t.Loop()");
}
```

Protip: To run a different file (e.g. Macro\_2.C), instead of making a new runme file, use the “sed” bash shortcut to exchange all instances of “Macro” in runme.C into “Macro\_2”:`$ sed -i "" 's/Macro/Macro_2/g' runme.C`(this is the Mac specific syntax for “sed”. See your own man page.)

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [June 12, 2013, 8:27pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/11 "2013-06-12T20:27:27Z")

</div>

You can pass command-line arguments from your shell to your named macro as well. The macro sees it as a char \*. Here is an old post of mine doing that: [url][TString, stdarg, variable argument list?](https://root-forum.cern.ch/t/tstring-stdarg-variable-argument-list/15001/1)

So you could have your runme.C look like:

```auto
#include "TString.h"
#include "TROOT.h"

void runme(char * arg){
gROOT->ProcessLine(TString::Format(".L %s.C",arg);
gROOT->ProcessLine(TString::Format("%s t",arg);
gROOT->ProcessLine("t.Loop()");
}
```

then as long as your macro/macro\_2 defines a class with a Loop() method, you can call any of your macros with something like:

```auto

```

---

<div class="post-metadata">

**Author:** ![bmesserl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/7cd45c/32.png) [@bmesserl](https://root-forum.cern.ch/u/bmesserl)\
**Post date:** [June 12, 2013, 8:54pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/12 "2013-06-12T20:54:18Z")

</div>

Even better, thanks J.F.

---

<div class="post-metadata">

**Author:** ![Pak](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/4bbf92/32.png) [@Pak](https://root-forum.cern.ch/u/Pak)\
**Post date:** [March 9, 2017, 9:15pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/13 "2017-03-09T21:15:29Z")

</div>

with your written .cpp file, say foo.cpp as:  
’#include   
using namespace std;  
void foo( ) {  
…  
}

you can compile and run the .cpp from command prompt by single line  
root -q -l foo.cpp

-q will automatically exit the ROOT prompt after compile.

You can also write a Makefile. This allows you to run series of processes.  
For example in Makefile, do:

.PHONY: run

run:  
root -q -l foo.cpp && python [foo.py](http://foo.py)

---

<div class="post-metadata">

**Author:** ![Rutik](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@Rutik](https://root-forum.cern.ch/u/Rutik)\
**Post date:** [August 10, 2022, 6:58pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/14 "2022-08-10T18:58:00Z")

</div>

Hi @Pepe_Le_Pew @Wile_E_Coyote @jfcaron ,  
I have a very similar situation as this post here. When I do run a macro of similar kind,  
I’m attaching the macro I use for this.  
[RunMixLoop.C](https://root-forum.cern.ch/uploads/short-url/sjpRvYpjCKPquABmUsxWh9oWhlC.C) (278 Bytes)  
I get the error

````auto
Error: Symbol PtCorr_mix t is not defined in current scope (tmpfile):1:
***Interpreter error recovered***
Error: Symbol t is not defined in current scope (tmpfile):1:
Error: Failed to evaluate t.Loop()
***Interpreter error recovered*** ```
````

---

<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:** [August 10, 2022, 7:15pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/15 "2022-08-10T19:15:29Z")

</div>

Is the “`PtCorr_mix`” class defined in the “`PtCorr_mix.C`” file?

---

<div class="post-metadata">

**Author:** ![Rutik](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@Rutik](https://root-forum.cern.ch/u/Rutik)\
**Post date:** [August 10, 2022, 7:59pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/16 "2022-08-10T19:59:19Z")

</div>

Hi @Wile_E_Coyote ,  
Thank you for the quick reply.  
Hmm, there is the definition of `#define PtCorr_mix_cxx`  
Here’s the file  
[PtCorr\_mix.C](https://root-forum.cern.ch/uploads/short-url/7gkf7T1VJntvexlxcVSNFSMTILV.C) (5.9 KB)  
[PtCorr\_mix.h](https://root-forum.cern.ch/uploads/short-url/4GXjiNLhxGChW47b0TbGZehHjsw.h) (4.3 KB)

---

<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:** [August 10, 2022, 8:13pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/17 "2022-08-10T20:13:15Z")

</div>

I guess you have bugs in your “`PtCorr_mix.C`” file, so it is not properly loaded.  
For example, the source code after “`}// Mixed event loop ends`” is a lulu.

Try to load it with ACLiC (fix all reported problems):  
`root [0] .L PtCorr_mix.C++`

---

<div class="post-metadata">

**Author:** ![Rutik](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@Rutik](https://root-forum.cern.ch/u/Rutik)\
**Post date:** [August 11, 2022, 5:15pm UTC](https://root-forum.cern.ch/t/compile-and-run-from-command-prompt/13799/18 "2022-08-11T17:15:04Z")

</div>

@Wile_E_Coyote  
Thank you!, I hope it is this silly mistake that’s causing the issue.

Cheers,  
Rutik
