Write cutom object in a standalone program

I’ve written a simply object LinearFitData:

#ifndef __LinearFitData__
#define __LinearFitData__

#include <TNamed.h>
#include <TBuffer.h>

class LinearFitData : public TNamed
{
private:
    UInt_t n;
    Double_t sumx; // sum of x
    Double_t sumy; // sum of y
    Double_t sumx2; // sum of squared x
    Double_t sumxy; // sum of x times y

public:
    LinearFitData();
    LinearFitData(const LinearFitData & other);
    LinearFitData& operator=(const LinearFitData & other);
    LinearFitData(const char* name, const char* title);
    virtual ~LinearFitData() { };
    UInt_t get_n() const { return n; }
    void push_back(double x, double y);

    ClassDef(LinearFitData, 1);
};
#endif
#include "linearfit_data.h"

ClassImp(LinearFitData)
...

now I’m able to write and read LinearFitData object to and from ROOT files when I compile it with aclic. Now I want to use it in a standalone program like:


#include <TFile.h>
#include "linearfit_data.h"

int main()
{
    LinearFitData x("nome", "titolo");
    x.push_back(1,2);
    TFile file("output.root", "RECREATE");
    x.Write();
    file.Close();
    return 0;
}

the problem is that I’m not able to link it:

g++ -o write -pthread -m64 -I/usr/local/root/include -L/usr/local/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic write.cpp linearfit_data.cpp

/tmp/ccOZwbus.o: In function `LinearFitData::~LinearFitData()':
write.cpp:(.text._ZN13LinearFitDataD1Ev[LinearFitData::~LinearFitData()]+0xd): undefined reference to `vtable for LinearFitData'
/tmp/ccIJhbXt.o: In function `__static_initialization_and_destruction_0(int, int)':
linearfit_data.cpp:(.text+0xc6): undefined reference to `ROOT::GenerateInitInstance(LinearFitData const*)'
/tmp/ccIJhbXt.o: In function `LinearFitData::LinearFitData(LinearFitData const&)':
linearfit_data.cpp:(.text+0x186): undefined reference to `vtable for LinearFitData'
/tmp/ccIJhbXt.o: In function `LinearFitData::LinearFitData(LinearFitData const&)':
linearfit_data.cpp:(.text+0x200): undefined reference to `vtable for LinearFitData'
/tmp/ccIJhbXt.o: In function `LinearFitData::LinearFitData(char const*, char const*)':
linearfit_data.cpp:(.text+0x282): undefined reference to `vtable for LinearFitData'
/tmp/ccIJhbXt.o: In function `LinearFitData::LinearFitData(char const*, char const*)':
linearfit_data.cpp:(.text+0x2f4): undefined reference to `vtable for LinearFitData'
/tmp/ccIJhbXt.o: In function `LinearFitData::LinearFitData()':
linearfit_data.cpp:(.text+0x356): undefined reference to `vtable for LinearFitData'
/tmp/ccIJhbXt.o:linearfit_data.cpp:(.text+0x3b8): more undefined references to `vtable for LinearFitData' follow
collect2: ld returned 1 exit status

What am I forgetting?

Hi,

You need to generate, compile and load a dictionary.
See root.cern.ch/drupal/faq#n676

Cheers,
Philippe.

[quote=“pcanal”]Hi,

You need to generate, compile and load a dictionary.
See root.cern.ch/drupal/faq#n676

Cheers,
Philippe.[/quote]

Ok, I did it, but I still have a problem

#ifdef __CINT__
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ nestedclasses;

#pragma link C++ class LinearFitData+;

#endif
rootcint -f DictOutput.cxx -c linearfit_data.h Linkdef.h

here I needed to add the -fPIC options:

g++ -shared -fPIC -o libLinearFitData.so `root-config --ldflags` -pthread -m64 -I/usr/local/root/include DictOutput.cxx linearfit_data.cpp
g++ -o write -L/usr/local/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic -pthread -m64 -I/usr/local/root/include write.cpp libLinearFitData.so
./write
./write: error while loading shared libraries: libLinearFitData.so: cannot open shared object file: No such file or directory

Hi,

Most likely the directory in which libLinearFitData.so is, is not in your LD_LIBRARY_PATH.

Cheers,
Philippe.

[quote=“pcanal”]Hi,

Most likely the directory in which libLinearFitData.so is, is not in your LD_LIBRARY_PATH.

Cheers,
Philippe.[/quote]

No, it isn’t, and I don’t want to add it. I solved compiled it into a static .o and not in a .so. Is there an alternative solution?

Hi,

It might also work to add the shared library full path to the link line.

[quote] and I don’t want to add it.[/quote]Well, shared library must be on the LD_LIBRARY_PATH and/or equivalent, so you would either need to install your library on one of the existing path or indeed use a static library.

Cheers,
Philippe.