字典用例
这是在 CMake 中构建包含字典模块的例子。通过 find_package
手动添加 ROOT,而非使用 ROOT 建议的标志。在大多数系统中,CMake 文件中唯一重要的标志。
examples/root-dict/CMakeLists.txt
cmake_minimum_required(VERSION 3.4...3.21)
project(RootDictExample LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard to use")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_PLATFORM_INDEPENDENT_CODE ON)
find_package(ROOT 6.20 CONFIG REQUIRED)
# If you want to support <6.20, add this line:
# include("${ROOT_DIR}/modules/RootNewMacros.cmake")
# However, it was moved and included by default in 6.201
root_generate_dictionary(G__DictExample DictExample.h LINKDEF DictLinkDef.h)
add_library(DictExample SHARED DictExample.cxx DictExample.h G__DictExample.cxx)
target_include_directories(DictExample PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(DictExample PUBLIC ROOT::Core)
## Alternative to add the dictionary to an existing target:
# add_library(DictExample SHARED DictExample.cxx DictExample.h)
# target_include_directories(DictExample PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
# target_link_libraries(DictExample PUBLIC ROOT::Core)
# root_generate_dictionary(G__DictExample DictExample.h MODULE DictExample LINKDEF DictLinkDef.h)
支持文件
这是一个极简的类定义,只有一个成员函数:
examples/root-dict/DictExample.cxx
#include "DictExample.h"
Double_t Simple::GetX() const {return x;}
ClassImp(Simple)
examples/root-dict/DictExample.h
#pragma once
#include <TROOT.h>
class Simple {
Double_t x;
public:
Simple() : x(2.5) {}
Double_t GetX() const;
ClassDef(Simple,1)
};
还需要一个 LinkDef.h
。
examples/root-dict/DictLinkDef.h
// See: https://root.cern.ch/selecting-dictionary-entries-linkdefh
#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 Simple+;
#endif
进行测试
这是一个宏示例,用于测试上面文件生成的结果是否正确。
examples/root-dict/CheckLoad.C
{
gSystem->Load("libDictExample");
Simple s;
cout << s.GetX() << endl;
TFile *_file = TFile::Open("tmp.root", "RECREATE");
gDirectory->WriteObject(&s, "MyS");
Simple *MyS = nullptr;
gDirectory->GetObject("MyS", MyS);
cout << MyS->GetX() << endl;
_file->Close();
}