CppMicroServices

C++ Micro Services: Getting Started
Getting Started

Projects which want to make use of the capabilities provided by the C++ Micro Services library need to set-up the correct include paths and link dependencies. Further, each executable or shared library which needs a ModuleContext instance must contain specific initialization code.

The C++ Micro Services library provides CMake utility functions for CMake based projects but there are no restrictions on the type of build system used for a project.

CMake based projects

To easily set-up include paths and linker dependencies, use the common find_package mechanism provided by CMake:

project(CppMicroServicesExamples)
cmake_minimum_required(VERSION 2.8)
find_package(CppMicroServices NO_MODULE REQUIRED)
include_directories(${CppMicroServices_INCLUDE_DIRS})

The CMake code above sets up a basic project (called CppMicroServicesExamples) and tries to find the CppMicroServices package and subsequently to set the necessary include directories. Building a shared library might then look like this:

# The library name for the module
set(_lib_name dictionaryservice)
# A list of source code files
set(_srcs
Activator.cpp
IDictionaryService.cpp
)
# Generate module initialization code
NAME "Dictionary Service"
LIBRARY_NAME ${_lib_name})
# Create the library
add_library(Example-${_lib_name} SHARED ${_srcs})
# Link the CppMicroServices library
target_link_libraries(Example-${_lib_name} ${CppMicroServices_LIBRARIES})

The call to usFunctionGenerateModuleInit generates the proper module initialization code and provides access to the module specific ModuleContext instance.

Makefile based projects

The following Makefile is located at examples/makefile/Makefile and demonstrates a minimal build script:

CXX = g++
CXXFLAGS = -g -Wall -Wno-unused -pedantic -fPIC $(CppMicroServices_CXX_FLAGS)
LDFLAGS = -Wl,-rpath=$(CppMicroServices_ROOT)/lib -Wl,-rpath=.
LDLIBS = -lCppMicroServices
INCLUDEDIRS = -I$(CppMicroServices_ROOT)/include/CppMicroServices
LIBDIRS = -L$(CppMicroServices_ROOT)/lib/CppMicroServices -L.
all : main libmodule.so
main: libmodule.so main.o
$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS) $(INCLUDEDIRS) $(LIBDIRS) $(LDLIBS) -lmodule
libmodule.so: module.o IDictionaryService.o
$(CXX) -shared -o $@ $^ $(CXXFLAGS) $(LDFLAGS) $(INCLUDEDIRS) $(LIBDIRS) $(LDLIBS)
main.o: main.cpp
$(CXX) $(CXXFLAGS) $(INCLUDEDIRS) -c $< -o $@
%.o: %.cpp
$(CXX) $(CXXFLAGS) -DMODULE_EXPORTS $(INCLUDEDIRS) -c $< -o $@
.PHONY : clean
clean:
rm -f *.o

The variable CppMicroServices_ROOT is an environment variable and must be set to the CppMicroServices installation directory prior to invoking make. The module initialization code for the libmodule.so shared library is generated by using the US_INITIALIZE_MODULE pre-processor macro at the end of the module.cpp source file (any source file compiled into the module would do):

#include <usModuleInitialization.h>
US_INITIALIZE_MODULE("My Module", "module")