GT.M binding to C++


GT.M binding to C++


M is both a language and a database. Just like with most other popular NoSQL databases it is useful to be able to access M from many different languages, even though using it from the M language might be the preferred approach. Bindings to other languages make the M database available to a larger number of applications and facilitates the growth of a larger community.


A C++ module has now been created to make possible to use GT.M from C++ programs.

This C++ module builds upon the gtm_cip C-API of GT.M.


Details of this C++ API for GT.M are available in this article in the OSEHRA Technical Journal.


The C++ module is based on a class “GTM”.

It is expected that GT.M will already be installed in the target system.

In order to use this API at run time you should do the following installation:


A) Set the GTMCI environment variable to the full path of the file.

B) Copy the _gtmaccess.m file in to one of the directories where you have M routines.
     These directories are the ones that you already include in the gtmroutines environment variable.

C) Add to your LD_LIBRARY_PATH environment variable the directory where you installed GT.M.

More precisely, the directory where the GT.M library


is currently located. This will make possible for the C++ binding to find the GT.M shared library when you run python scripts. This directory is what you would typically have in the "gtm_dist" environment variable.



Once you have completed the post-installation, you can try some of the .cpp tests available at


For example, start with C++ code such as:


#include "GTM.h" #include <cstdlib> #include <iostream> int main( int argc, char * argv [] ) { GTM * gtm = new GTM(); const char * version = gtm->Version(); std::cout << "Version = " << version << std::endl; const char * about = gtm->About(); std::cout << "About = " << about << std::endl; delete gtm; return EXIT_SUCCESS; }

Then follow with more interesting code, such as:


#include "GTM.h" #include "GTMException.h" #include <cstdlib> #include <iostream> int main( int argc, char * argv [] ) { GTM gtm; try { gtm.Set( "^FibonacciA", "1" ); gtm.Set( "^FibonacciB", "1" ); std::string getValue = "Initially empty"; for( unsigned int i = 0; i < 10; i++ ) { gtm.Execute("set ^FibonacciValue=^FibonacciA+^FibonacciB"); gtm.Execute("set ^FibonacciB=^FibonacciA"); gtm.Execute("set ^FibonacciA=^FibonacciValue"); gtm.Get( "^FibonacciValue", getValue ); std::cout << "Fibonacci value = " << getValue << std::endl; } gtm.Kill( "^FibonacciA" ); gtm.Kill( "^FibonacciB" ); gtm.Kill( "^FibonacciValue" ); } catch( GTMException & excp ) { std::cerr << excp.what() << std::endl; } return EXIT_SUCCESS; }

Enjoy GT.M from C++ !




Measuring C++ binding performance

Luis Ibanez's picture

For an quick evaluation of comparative performance,
Please see: