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
Procedure
- Setup a C++ implementation project if your SPL project
does not already have one.
- 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.
- Create a file that is named Makefile in the Project Explorer <SPL Project>/Resources/impl folder.
- Add a single-line all: to the Makefile and save it.
- In the Project Explorer, select the C++ project and right-click.
- In the menu, select Build Configurations > Build All. The project builds without the earlier C++ project build problems.
- 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.
- Create the new native function model as described in Creating a sample native function without a linked C++ project.
- 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.
- Create a main composite to test the native function as described in Creating a sample native function without a linked C++ project.
- Create and launch a stand-alone build of the test application as described in Creating a sample native function without a linked C++ project.
- If your native function implementation includes external library dependencies, modify your model as described in Specifying dependent libraries for C++ native functions.
- 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.
- 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_ */ - 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; } } - 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 - Modify your native function model to declare this library:
- Open the Native Function Model Editor.
- In the editor's tree view, navigate to and select the node Native Functions > Function Set > Libraries and right-click.
- From the menu, select New Child > Library.
- Select the new Library and right-click.
- From the menu, select Show Properties View.
- 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 - Save the modified model.
- 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); } - 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
- Create and populate Project Explorer file <SPL
Project>/Resource/impl/include/MySampleLib.h with the following
code:
Parent topic: Creating C++ native functions
Related information: