XI How
to compile your own code for VEGA or use the VEGA libraries in your code
XI.1 Loading
an existing shared library
If a shared library that was suitably prepared (we will see what that
means) exists already, it is possible to extend VEGA by loading it. The command
to do that is “.L” as in
vega[] .L scrollspectrum.so
Once this has been done, the functions and methods that are defined in the
library are available at your fingertips in the interpreter.
What means “suitably prepared” is that one has to build the
stub functions that make the interface between the interpreter and the real
functions. This is however an automatic process that is using a tool called
rootcint. We will describe it more extensively later.
XI.2 Compiling
a script and making a shared library of it
The script compiler was introduced in the chapter “ How to compile a
macro?”. Before going further, let’s remind what was said.
Sometimes (in fact quite often) a script may become so big or be so
processing-hungry that one would like to have it compiled and not interpreted.
There is included into ROOT what is called a script compiler that allows
compiling very easily the scripts you are going to produce.
It starts by calling the C++ compiler that was used to build ROOT and
builds a shared library. This shared library is then loaded and the function
defined in the script is executed.
The shared library that is produced is going to stay and you will be able
to extend VEGA by loading this library later.
But before using the script compiler, one has to prepare a little bit his
script, though it is quite simple.
XI.2.1 Preparation
of the script
You have to
- Do not use extensions to C/C++ provided by the CINT interpreter. That means
for example that while in the interpreter you do not need to declare variables
whose type is known from the context, this is mandatory if you want to be able
to compile. Since the compiler is not supposed to know the context of the
interpreter. An example. In the interpreter, you can use statements like
:
c1 = new TCanvas("c1", "Test",1);
where you do not
declare the variable c1, you should do instead :
TCanvas*
c1 = new TCanvas("c1", "Test",1);
More generally, use very standard
C/C++ and declare all your variables
- You should add the includes you use at the beginning of the file. Once
again, this is for the real compiler that will do the work. Usually, the headers
to be added are composed of the name of the classes used followed by the .h
suffix. In case of included C code, the most commonly used are
"FrameL.h" for all the Framelib calls, and the standard headers of the
Virgo standard signal library.
- In case you want to execute immediately the first function of the script you
compile, you have to name the function in your script with the same name as the
file it is contained in. For example the file my_test.C should begin
with
my_test()
{
your
code....
}
The tutorial script called scrollspectrum.C has been prepared this
way, check it.
XI.2.2 Compiling
the script
What remains to be done is to compile the script. If we want to execute the
script called scrollspectrum.C, we just have to do (once in the
vegatutorial directory):
vega[] .x scrollspectrum.C++
notice the ++ at the end of the command. This tells the interpreter to
compile scrollspectrum.C, build a shared library called
scrollspectrum.so, load this shared lib and execute
scrollspectrum().
The resulting library scrollspectrum.so stays here and you can reuse it,
that is reload it at a later time, with
vega[] .L scrollspectrum.so
furthermore, the scrollspectrum() function is available in the interpreter
and may be used in some other script!
All this also works with loading the script after compiling:
vega[] .L scrollspectrum.C++
is a valid statement. This will build the library and load it as before. It
will just not execute any function.
XI.2.3 Limitations
The script compiler is not intended to be used for big projects. Do not
expect to compile libraries that are made of many subroutines in many different
source files. It is quite efficient though for checking that a code will compile
before effectively using it.
It is also possible to overcome some CINT limitations using the script
compiler since it uses the full-featured compiler you are used to
have.
XI.3 Building
a standalone executable
XI.3.1 Building
an executable that is linked with the ROOT and VEGA libraries
The directory “test” contains a few examples of programs that
use the VEGA and ROOT classes. The “main–metadb.cc”
and “main-display.cc” programs are just skeletons that may
be used as a starting point to build bigger programs.
The main difficulty for their compilation is to put all the necessary
includes and to link with all the necessary libraries. Furthermore, one has to
use the same compiler as the one used for compiling ROOT and VEGA. This ensures
the less possible problems.
In order to simplify as much as possible the determination of the various
includes, libraries or parameters to use, a utility script is provided, called
“vega-config” that shows them. This utility is generated at
VEGA compile time and returns all the parameters used for compiling
VEGA.
For example, to determine what C++ compiler was used, do:
vega[] vega-config --cxx
g++
XI.3.2 Options
of the vega-config utility
The options available for “vega-config” are the
following:
--help : prints a help message
--metadb-set : only output parameters (includes, libraries) relevant to
metadatabase for the following options
--display-set : only output parameters (includes, libraries) relevant to
display for the following options
--signal-set : only output parameters (includes, libraries) relevant to
signal for the following options
an OR is made of the three previous flags
--cxx : command to invoke the C++ compiler
--cc : command to invoke the C compiler
--ld : command to invoke the linker
--prefix : gives the prefix path used for root
--cflags : outputs the flags used by the C/C++ compiler to compile root and
VEGA
--ldflags : flags used when invoking the linker
--soflags : flags invoked for creation of a shared object
--coptflags : outputs the optimisation flag used by the C/C++ compiler to
compile root and VEGA
--includes : output include options used when compiling an object for VEGA,
following the previous flags
--nonew : do not include libNew for the following options
--libs : output libraries options used when compiling an object for VEGA,
following the previous flags
--glibs : output libraries used by VEGA or ROOT when making an application
using the ROOT GUI
--defines : output defines that were used for compiling VEGA following the
previous flags
--version : gives the VEGA version
The flags may be combined to give a complete command and characters that do
not represent any flag are output as is. Examples :
> vega-config --cxx -c toto.cc
g++ -c toto.cc
One can build a complete command to compile a source code :
> vega-config --cxx --cflags --includes -c toto.cc
g++ -fPIC –fsigned-char –I/home/toto/vega/src/include
–I/virgoApp/Fr/v4r25/src –I/home/toto/root/include
–I/home/toto/fftw/include –c toto.cc
One clearly sees that the result may not be simple, and that it depends on
the particular configuration of the system used. The vega-config command and
flags are the same however and insure that one is able to compile his program
the same way whatever the system.
The vega-config script only outputs the command, but does not execute it.
In most shells, putting the command between back quotes will execute the command
and then the output of the command :
> `vega-config --cxx --cflags --includes -c toto.cc`
Now, it is possible to build relatively easily any program. First try with
the ones provided in the test directory.
XI.3.3 Using
only a subset of capabilities
To allow saving of resources, one may be willing to use only relevant parts
of the VEGA/ROOT system. For example use only metadatabase, without graphics and
display. There are three flags of the vega-config utility that restrict the
output to only relevant parts of a particular set :
--metadb-set : only output parameters (includes, libraries) relevant to
metadatabase for the following options
--display-set : only output parameters (includes, libraries) relevant to
display for the following options
--signal-set : only output parameters (includes, libraries) relevant to
signal for the following options
Try yourself the following commands and see the difference :
> vega-config --includes –libs
> vega-config –metadb-set --includes --libs
XI.4 Building
a general shared library usable with VEGA
Table of Contents
Damir BUSKULIC
Last update :19/11/2001;