Hi jonas
Thanks for your answer. I download the code in git and follow it’s steps, but I still meet some questions.
cmake_minimum_required(VERSION 3.9)
project (readwrite_twoints CXX)
message(STATUS “Looking for ROOT”)
find_package(ROOT REQUIRED COMPONENTS RIO)
add_library(readwrite_twoints SHARED readwrite_twoints.cpp twoints.cpp )
target_link_libraries(readwrite_twoints ROOT::RIO)
target_include_directories(readwrite_twoints PRIVATE
${CMAKE_CURRENT_SOURCE_DIR})
ROOT_GENERATE_DICTIONARY(twoints_dict twoints.hpp MODULE readwrite_twoints LINKDEF LinkDef.h)
this is my CMakeLists.txt, I only change the add_executable to add_library, and I get a libreadwrite_twoints.so, and I change main function to “f” function in readwrite_twoints.cpp
#include “TFile.h”
#include “twoints.hpp”
#include
int main(){return 0;}
int f() {
{
// write the ti
object to an output file
TwoInts ti(1, 2);
TFile out_f(“f.root”, “recreate”);
out_f.WriteObjectAny(&ti, “TwoInts”, “ti”);
std::cout << “Written TwoInts(” << ti.GetA() << ", " << ti.GetB() << “)\n”;
}
TFile in_f(“f.root”);
auto *tiptr = in_f.Get(“ti”);
std::cout << “Read TwoInts(” << tiptr->GetA() << ", " << tiptr->GetB() << “)\n”;
return 0;
}
and I write a test.py in order to call this “f” function
import ROOT
ROOT.gSystem.Load('./build/libreadwrite_twoints.so')
ROOT.f()
but it doesn’t work
Traceback (most recent call last):
File “/scratchfs/cepc/wangshengyi/test/cmake2/test.py”, line 5, in
ROOT.f()
File “/cvmfs/sft.cern.ch/lcg/releases/ROOT/6.24.06-3455f/x86_64-centos7-gcc8-opt/lib/ROOT/_facade.py”, line 192, in _fallback_getattr
raise AttributeError(“Failed to get attribute {} from ROOT”.format(name))
AttributeError: Failed to get attribute f from ROOT
but it work when I add this line in my python file
ROOT.gInterpreter.ProcessLine('#include "./readwrite_twoints.cpp"')
I think using linkdef can omit this line, or Is my usage wrong?