1.2. Hello, World!

The learning curve of any library, especially an object-oriented library is always steep. I discovered that the best way to help people get accustomed with a new library is to have a utility that would generate at least rudimentary code for them to begin with. The code should be self-sufficient and it should compile out of the box.

In order to get a feel of what using libassa buys you as a programmer, we are going to write a program that prints out Hello, World! phrase to the standard output and then look more closely into the details.

libassa comes with a utility, assa-genesis, that generates an application shell code. It comes with the library and is installed during an installation step.

To generate the code, create a test directory and execute the following command:

% mkdir helloworld
% cd helloworld
% assa-genesis --one-file HelloWorld
	

This says to generate libassa application skeleton and put it into one file (--one-file option) with the name HelloWorld.cpp.

Note

To find out other command-line argument of assa-genesis utility, try % assa-genesis --help.


% assa-genesis --one-file HelloWorld

assa-genesis: Generating skeleton files ...

Created: "HelloWorld.cpp"
	  

Open HelloWorld.cpp in your favorite editor and locate function processServer(). This function is responsible for the data processing phase of your program. Modify it by commenting out the even processing loop and adding the printout statement.


#include <iostream>

void
HelloWorld::
processServer ()
{
    trace("HelloWorld::processServer");

    // This is the event processing loop 
    //
    // while (!stopServer()) {
    //      m_reactor.waitForEvents ();
    // }

    std::cout << "Hello, World!" << std::endl;

    // Shut the service down
    m_reactor.stopReactor ();
    DL((ASSA::APP,"Service stopped!\n"));
}

	

To compile, we are going to use assa-config utility that provides appropriate compilation flags. This utility is installed along with the library.

% g++ `assa-config --cflags` HelloWorld.cpp -o HelloWorld `assa-config --libs`
	

Note

To find out other command-line argument of assa-config utility, try %assa-config --help

Run the program and observe the result:

% ./HelloWorld

Hello, World!
	

1.2.1. Using assa-genesis Utility

We used --one-file option to put all generated code into one file. By default, however, assa-genesis generates four different files. In our case they would be:

% assa-genesis HelloWorld

% ls

HelloWorld-main.cpp
HelloWorld-main.h

HelloWorld.cpp
HelloWorld.h
	  

The first two are the header and C++ file that encapsulate the main() and embed help which you get with --help option. The other two files are the declaration and definition of HelloWorld class that encapsulates our application.

Add "Hello, World!" printout statement to the same function in HelloWorld.cpp. To compile the files, use the following set of commands:

% g++ `assa-config --cflags` HelloWorld.cpp -c
% g++ `assa-config --cflags` HelloWorld-main.cpp -c
% g++ HelloWorld.o HelloWorld-main.o -o HelloWorld `assa-config --libs`
	  

You can, instead, use the following Makefile (if you cut-n-paste, make sure you replace spaces with TABs where appropriate).


# Makefile for HelloWorld

CFLAGS=`assa-config --cflags`
LIBS=`assa-config --libs`

HelloWorld: HelloWorld.o HelloWorld-main.o
        g++ $(CFLAGS) $? -o HelloWorld $(LIBS)

.cpp.o:
        g++ $(CFLAGS) -c $<

clean:
        -rm -f *.o *~ HelloWorld

HelloWorld.o: HelloWorld.cpp HelloWorld.h
HelloWorld-main.o: HelloWorld-main.cpp HelloWorld-main.h
	  

We are going to use this example in the discussion that follows in the next chapter.