Function Call Through Symbolic Link in Root Directory Causing Trouble

I’m following some tutorials to practice, but I’m encountering errors when compiling a C++ file in my user directory on Ubuntu.

#include "TMath.h"
double Maxwell(double *x, double *par)
{
if(x[0] > par[1] && par[2] > 0 && par[0] > 0)
{
return par[0]*(x[0]-par[1])/par[2]*
        TMath::Exp(-(x[0]-par[1])/par[2]);
}
else
{
        return 0.;
}
}

I have clearly marked the header files, but after compilation:

g++ -o TestMaxwell TestMaxwell.cpp

there are errors reported:

TestMaxwell.cpp:1:10: fatal error: TMath.h: No such file or directory
    1 | #include "TMath.h"
      |          ^~~~~~~~~
compilation terminated.

I have also installed the Boost library:

sudo apt-get install libboost-all-dev

At the same time, there are errors reported when calling the link.

root[0] .L Maxwell.C+
Error in <TRint::ProcessLine>: macro Maxwell.C not found in path .:/home/linbo/root/macros

I’m a little confused. Can the functions I wrote in the Ubuntu user interface be linked to under root?

I’m embarrassed to ask such basic questions, but after searching the internet for a long time without finding solutions to my problems, I’ve decided to post a topic in the forum. Do you have any good root introductory videos or PDF files on learning astronomical data fitting? I would be very grateful.

Hi,

Thanks for the question. You are missing some flags in your compiler invocation. Did you maybe try

g++ -o TestMaxwell TestMaxwell.cpp `root-config --libs --cflags`

?

I hope this helps!

Best,
D

I tried this ,first show like this:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

So I added an empty main function:

#include "TMath.h"

double Maxwell(double *x, double *par)
{
if(x[0] > par[1] && par[2] > 0 && par[0] > 0)
{
return par[0]*(x[0]-par[1])/par[2]*
        TMath::Exp(-(x[0]-par[1])/par[2]);
}
else
{
        return 0.;
}
}
int main() {
               return 0;
}

Fortunately, it did generate a file.

TestMaxwell  TestMaxwell.cpp

I tried to link it in root

root [0] .L Maxwell.C+
Error in <TRint::ProcessLine>: macro Maxwell.C not found in path .:/home/linbo/root/macros

I followed the tutorial from [root_for_beginners_Day3], but I was unable to load it successfully. I’m not sure how they did it, and the link to this portion of the tutorial is [http://caeinfo.in2p3.fr/root/Formation/en/Day3/Maxwell.C], and I was unable to open it as well.

Here you should invoke the Maxwell function though, right?

Cheers,
D

Ok, thanks a lot for answering my stupid question hah.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.