IBM Streams 4.2.1

Creating a sample native function with a linked C++ project

Use this method if you want to use the Eclipse C++ development tools (CDT) to develop native function code.

About this task

This task involves creating a linked C++ project. The process is the same as the C++ project setup that is used when you develop C++ primitive operators that include additional C++ code.

Procedure

  1. Setup a C++ implementation project if your SPL project does not already have one.
    1. Follow the steps in Setting up for C++ operator development but omit the steps of creating a new C++ Primitive operator and creating a Makefile.
      Note: Ignore any build warning in the new C++ project for now.
    2. Create a file that is named Makefile in the Project Explorer <SPL Project>/Resources/impl folder.
    3. Add a single-line all: to the Makefile and save it.
    4. In the Project Explorer, select the C++ project and right-click.
    5. In the menu, select Build Configurations > Build All. The project builds without the earlier C++ project build problems.
  2. Create the new native function model as described in Creating a sample native function without a linked C++ project.
  3. Create the native function C++ header file as described in Creating a sample native function without a linked C++ project.
    Note: Ignore any warning in the editor view about the unresolved SPLFunctions.h header file.
  4. Create a main composite to test the native function as described in Creating a sample native function without a linked C++ project.
  5. Create and launch a stand-alone build of the test application as described in Creating a sample native function without a linked C++ project.
  6. If your native function implementation includes external library dependencies, modify your model as described in Specifying dependent libraries for C++ native functions.
  7. If your native function implementation includes additional C++ code, such as a C++ primitive operator implementation that has additional C++ code, create a shared library that contains that code. The following steps create a library libMySampleLib.so and modify the native function to use it.
    1. Create and populate Project Explorer file <SPL Project>/Resource/impl/include/MySampleLib.h with the following code:
      #ifndef MYSAMPLELIB_H_
      #define MYSAMPLELIB_H_
      
      #include "SPL/Runtime/Function/SPLFunctions.h"
      
      namespace mysamplelib {
        class MySampleLib {
       	 public:
      		   static SPL::int64 squareit(SPL::int64 const& x);
       	};
      }
      
      #endif /* MYSAMPLELIB_H_ */
    2. Create and populate Project Explorer file <SPL Project>/Resource/impl/src/MySampleLib.cpp with the following code:
      #include "MySampleLib.h"
      
      namespace mysamplelib {
      
        SPL::int64 MySampleLib::squareit(SPL::int64 const& x) { 
          std::cout << "mysamplelib::MySampleLib::squareit()" << std::endl; return x*x;
        }
      }
    3. Update your Project Explorer file <SPL Project>/Resource/impl/Makefile with the following code (replacing <tab> with a real tab):
      Note: When C++ libraries are built for use in an installation on IBM® Power Systems™, the Advance Toolchain compiler must be used. The compiler is installed with the Advance Toolchain RPMs, and is invoked by using the following commands:
      • /opt/at7.0/bin/g++ (big endian)
      • /opt/at8.0/bin/g++ (little endian)
      ifeq ($(PLATFORM),ppc64)
        CXX=/opt/at7.0/bin/g++
      else ifeq ($(PLATFORM),ppc64le)
        CXX=/opt/at8.0/bin/g++
      endif
      SPL_PKGCFG=$(STREAMS_INSTALL)/bin/dst-pe-pkg-config.sh
      SPL_PKG=dst-spl-pe-install
      SPL_INCLUDE_OPTIONS = `$(SPL_PKGCFG) --cflags $(SPL_PKG)`
      
      .PHONY: clean distclean
      
      all: lib/libMySampleLib.so
       
      src/MySampleLib.o: src/MySampleLib.cpp include/MySampleLib.h
      <tab>@echo Compiling 'MySampleLib.cpp' ...
      <tab>@$(CXX) -Wall -fPIC -I include $(SPL_INCLUDE_OPTIONS) -c src/MySampleLib.cpp -o $@
      
      lib/libMySampleLib.so: src/MySampleLib.o
      <tab>@echo Building C++ shared library '$@'
      <tab>@$(CXX) -shared -o $@ $<
      
      clean:
      <tab>rm -rf src/MySampleLib.o lib/libMySampleLib.so
      
      distclean: clean
    4. Modify your native function model to declare this library:
      1. Open the Native Function Model Editor.
      2. In the editor's tree view, navigate to and select the node Native Functions > Function Set > Libraries and right-click.
      3. From the menu, select New Child > Library.
      4. Select the new Library and right-click.
      5. From the menu, select Show Properties View.
      6. In the Properties view for the selected new Library node, specify the following values (assuming a native function model that is created in a top-level SPL namespace):
        Include Path: ../../impl/include
        Lib: MySampleLib
        Lib Path: ../../impl/lib
      7. Save the modified model.
    5. Modify your native function to use the new library code. In the file Project Explorer <SPL Project>/Resources/impl/include/Functions.h, replace the declaration for the sample f function with the following declaration and save the file.
       SPL::int64 f(SPL::int64 const & x) { return  mysamplelib::MySampleLib::squareit(x); }
    6. Rebuild your stand-alone SPL test application and restart it. The Console view reports:
      mysamplelib::MySampleLib::squareit()
      f(0)=0
      mysamplelib::MySampleLib::squareit()
      f(1)=1
      mysamplelib::MySampleLib::squareit()
      f(2)=4
      mysamplelib::MySampleLib::squareit()
      f(3)=9
      mysamplelib::MySampleLib::squareit()
      f(4)=16