Creation of a simple shared library

Hello there,

I am trying to follow this root.cern.ch/root/CintGenerator.html to compile a simple event class and I get the following error:

$ make
g++ -g +a1 +Z -I/usr/include/root -c -o Track.o Track.C
g++: +a1: No such file or directory
g++: +Z: No such file or directory
make: *** [Track.o] Error 1

My Makefile:

#---------------------------------------------------
CXXFLAGS = -g +a1 +Z -I/usr/include/root
LDFLAGS = -g +a1 -b
LD = CC

HDRS = Event.h Track.h eventdict.h

SRCS = Event.C Track.C eventdict.C

OBJS = Event.o Track.o eventdict.o

PROGRAM = event.sl

all: $(PROGRAM)

$(PROGRAM): $(OBJS)
@echo “Linking $(PROGRAM) …”
@/bin/rm -f $(PROGRAM)
@$(LD) $(LDFLAGS) $(OBJS) -o $(PROGRAM)
@chmod 555 $(PROGRAM)
@echo “done”

clean:; @rm -f $(OBJS) core

Event.o: Event.h
Track.o: Track.h

eventdict.C: Event.h Track.h
@echo “Generating dictionary …”
@rootcint eventdict.C -c Event.h Track.h
#---------------------------------------------------

My ROOT version is:
ROOT 5.18/00b (branches/v5-18-00-patches@22563, Apr 06 2010, 01:56:00 on linux)
CINT/ROOT C/C++ Interpreter version 5.16.29, Jan 08, 2008

I am on ubuntu 10.04. Let me know if you need more information.

Unfortunately it looks like the code on the page you used is for HP-UX. I have no experience with it, but it looks like the compiler options are different from the GNU C++ compiler which is what I assume you are using on Ubuntu. Also HP-UX uses .sl for shared library extension, whereas you probably want to use .so on Ubuntu.

I modified the files and got them to compile on Fedora 13 which should be similar enough to your Ubuntu setup:

#CXXFLAGS      = -g +a1 +Z -I$(ROOTSYS)/include
CXXFLAGS      = -g -I/usr/local/include/root
#LDFLAGS       = -g +a1 -b
LDFLAGS       = -g -shared
#LD            = CC
LD            = c++

HDRS          = Event.h Track.h eventdict.h

SRCS          = Event.C Track.C eventdict.C

OBJS          = Event.o Track.o eventdict.o

#PROGRAM       = event.sl
PROGRAM       = event.so
   
all:            $(PROGRAM)
   
$(PROGRAM):     $(OBJS)
        @echo "Linking $(PROGRAM) ..."
        @/bin/rm -f $(PROGRAM)
        @$(LD) $(LDFLAGS) $(OBJS) -o $(PROGRAM)
        @chmod 555 $(PROGRAM)
        @echo "done"

clean:;         @rm -f $(OBJS) core

###
Event.o: Event.h
Track.o: Track.h

eventdict.C: Event.h Track.h
        @echo "Generating dictionary ..."
        @rootcint eventdict.C -c Event.h Track.h

After compiling I started ROOT and did gSystem->Load("event.so") to see that it loaded the library and did something like Event *t = new Event() and Track *t = new Track() and saw that it instantiated those objects without complaint.

One odd quirk that I experienced on Fedora 13, that you may not see on Ubuntu was that when I first tried to load the library I got a memory error:

root [0] gSystem->Load("./event.so");
dlopen error: /home/whanlon/tmp/././event.so: cannot restore segment prot after reloc: Permission denied
Load Error: Failed to load Dynamic link library /home/whanlon/tmp/././event.so
*** Interpreter error recovered ***

This is apparently an effect of SELinux preventing users from manipulating memory in a way that may be suspicious. I had to turn off SELinux enforcing to allow the library to load using: sudo setenforce 0.
Track.C (541 Bytes)
Track.h (668 Bytes)
Event.C (682 Bytes)
Event.h (607 Bytes)

Thanks a lot, it works now.

Hi,

wow, you found an amazingly old page… We’ll get rid of it! http://root.cern.ch/drupal/content/interacting-shared-libraries-rootcint would have probably been more helpful.
Cheers, Axel.