#1 2012-10-02 11:07:55

andzoff
Member
Registered: 2012-09-24
Posts: 7

Singleton example

Hi Sascha and Hi everybody.
I'm trying to build/compile the Singleton snippet example.
I don't want to use CMake, I'd like to use my own makefile 'cause I need to understand smile

So:
1) I copied the files into my folder. Files are: SingletonOne.cpp  SingletonOne.h  SingletonTwo.cpp  SingletonTwo.h main.cpp

2) I read the Module Context documentation page here  and so...

3) I added the two lines:

#include <usModuleInitialization.h>
US_INITIALIZE_MODULE("Singleton Service", "SingletonServiceModule", "", "1.0.0")

at the beginning (more or less) of the SingletonOne.cpp file

4) I wrote my own Makefile:

CCC = gcc

CPPUS_HOME = $(HOME)/Develop/CppMicroServices

OBJDIR = objs

TESTDIR = test01

INCLUDEDIRS = -I. -I$(CPPUS_HOME)/include

OBJECTS = $(OBJDIR)/SingletonOne.o \
	  $(OBJDIR)/SingletonTwo.o

CXXFLAGS = -g -O2 -Wall -Wno-unused -pedantic

SYS_LIBS = -lc -lstdc++ -lpthread -ldl

LIBS = -L$(CPPUS_HOME)/lib -lCppMicroServices

TARGET = $(TESTDIR)/main

all : $(OBJDIR) $(OBJECTS)
	echo "Type <make test> to have a test executable"

$(OBJDIR):
	mkdir -p $@

$(TESTDIR):
	mkdir -p $@

$(OBJDIR)/%.o: %.cpp
	$(CCC) $(CXXFLAGS) $(INCLUDEDIRS) -c $< -o $@

$(TARGET).o: $(TARGET).cpp
	$(CCC) $(CXXFLAGS) $(INCLUDEDIRS) -c $< -o $@

test : $(TARGET)

$(TARGET) : $(TARGET).o
	$(CCC) $(CXXFLAGS) $(OBJECTS) $(TARGET).o -o $(TARGET) $(SYS_LIBS) $(LIBS)

backup:
	$(HOME)/backup.sh

.PHONY : clean
clean:
	rm -f *.o *~ $(TARGET).o $(TARGET)
	rm -rf $(OBJDIR)

4) I launched the make command.
The make command is only for the objects files (no linking) and got all ok.
The make test command is for linking/producing the executables. And It's not ok, the result is:

[andrea@thickasabrick Activator01]$ make test
gcc -g -O2 -Wall -Wno-unused -pedantic -I. -I<CppMicroServices-Home-Path>/include -c test01/main.cpp -o test01/main.o
gcc -g -O2 -Wall -Wno-unused -pedantic objs/SingletonOne.o objs/SingletonTwo.o test01/main.o -o test01/main -lc -lstdc++ -lpthread -ldl -L<CppMicroServices-Home-Path>/lib -lCppMicroServices
objs/SingletonOne.o: In function `SingletonOneService::GetInstance()':
<example-path>/SingletonOne.cpp:36: undefined reference to `us::GetModuleContext()'
objs/SingletonTwo.o: In function `SingletonTwoService::GetInstance()':
<example-path>/SingletonTwo.cpp:31: undefined reference to `us::GetModuleContext()'
/usr/bin/ld: test01/main: hidden symbol `us::GetModuleContext()' isn't defined
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status
make: *** [test01/main] Error 1
[andrea@thickasabrick Activator01]$ 

note that SingletonOne.cpp:36 is different from yours 'cause I added the lines as said in point 3).


Well, what did I wrong?
If you need the entire stuff, I'll send it to you.
Is not my intention to waste your time, so please answer to me only if you think tiz worthwile for the community and for your time.
Thank you for the attention.

Offline

#2 2012-10-02 16:33:33

sascha
Administrator
Registered: 2012-05-04
Posts: 46

Re: Singleton example

Hi there,

Not using CMake is perfectly okay with me smile

In fact, I would love to take your example Makefile (after we ironed out a few issues) to base a minimal Makefile code snippet on it and include it in the repository and documentation - if you are okay with that.

So what you did was actually mostly right, but you revealed a gap in the documentation and there are some other small issues. Let's go through them:

1.) Unfortunately, I could not reproduce the linking error with gcc 4.6 for the executable. What is your gcc version? I used your Makefile and it compiled and linked the executable successfully. The us::GetModuleContext() symbol should have been defined by the macro US_INITIALZE_MODULE(...) which you used correctly (well, sort of; see below). So could you please post the output of

nm -C objs/SingletonOne.o | grep GetModuleContext

2.) Because you link everything into the main executable, the arguments to US_INITIALZE_MODULE and US_EXPORT_MODULE_ACTIVATOR (in main.cpp) need to be adapted for the executable case: If US_INITIALIZE_MODULE is used in an executable, the second argument *must* be an empty string and the first argument must match the first argument in US_EXPORT_MODULE_ACTIVATOR. Further, that first argument must be a valid C identifier (no white space etc.). I will add that to the documentation.

3.) In order for the symbols in the executable (or shared library) to be found by the C++ Micro Services library, you need add "-rdynamic" to the CXXFLAGS variable. See also this topic here.

I can check the compilation on an older gcc later. If you keep having problems with the GetModuleContext() symbol, please send me your complete project.

Thanks.

Offline

#3 2012-10-04 10:31:20

andzoff
Member
Registered: 2012-09-24
Posts: 7

Re: Singleton example

Hi Sascha,
sorry for the delay, I was very busy. But now I'm here.

Of course, you can use the Makefile posted. I'm proud to give my little contribution to the project.

1) I'm going to send you my test package in order to get it work.

2) I do not understand. Maybe I've to study more (and I don't want to waste your time). I understand that:

  • a module should produce its own library, like myModule.so

  • for the file needed to have a module, I'd better to refer to the  libA example, in which there are three files: usTestModuleA.cpp, usTestModuleAService.h, TestModuleA.cpp. In this way, is easier where to put the US_INITIALIZE_MODULE macro. Maybe the Sibngleton example is not enough for this (for figure out where to put the macro, I mean).

I promise that I will apply on this. smile

3) OK, I added -rdynamic to the makefile. The error remains, but I realize -rdynamic is needed.

My compilers are gcc 4.1.2 on the RHEL and gcc 4.4.x on the CentOS 6.3.

Thanks.

Offline

#4 2012-10-04 11:45:43

andzoff
Member
Registered: 2012-09-24
Posts: 7

Re: Singleton example

Something begins to work!

In my Makefile project I forgot to insert the uServices_singleton_init.cpp file.
I inserted it. I changed the makefile from (only the fragment is reported):

OBJECTS = $(OBJDIR)/SingletonOne.o \
	  $(OBJDIR)/SingletonTwo.o

to

OBJECTS = $(OBJDIR)/SingletonOne.o \
	  $(OBJDIR)/SingletonTwo.o \
	  $(OBJDIR)/uServices_singleton_init.o

well,
now it compile and runs. I'm very happy, Tiz a great result for me.

The next step is to realize my test project from scratch. And I will modify the makefile in order to produce the module's library. Maybe in this step I'll need your help.
Thank you Sascha for your support.
Andrea

Offline

#5 2012-10-04 13:12:57

sascha
Administrator
Registered: 2012-05-04
Posts: 46

Re: Singleton example

Good to hear that you are making progress.

However, now I don't understand smile

Where did you get the source for the uServices_singleton_init.o object file? This should not be needed... could you send me your current project to webmaster at cppmicroserves.org please?

Thanks for keeping trying and don't hesitate to ask more questions.

Offline

#6 2012-10-04 17:18:35

andzoff
Member
Registered: 2012-09-24
Posts: 7

Re: Singleton example

Hi,
I found the uServices_singleton_init.cpp file at the

<CppMicroServices>/documentation/snippets

path.
I downloaded the saschazelzer-CppMicroServices-7d5ecec.tar.gz file on Sep the 28th from the usual site.

In a private message is arriving my complete project pack.
Greetings from Rome.

Offline

Board footer

Powered by FluxBB