New York kdb Training 2019
Spaces still available. Register now to learn Kdb+.

Extend Kdb using Compiled C DLL Example

It is possible to import C functions directly for use within the kdb process by dynamically loading modules. Below we will look at an example of compiling, importing and executing C functions on windows for kdb 3.0+.

We will be adding two functions myavg, mysum which provide moving average and moving sum functions respectively. Yes, kdb does already mavg but read till the end and I think you will find the performance comparison interesting. The Interfacing Kdb with C and Extending with C page on the code KX website documents the C API extensively, we will give details for the core parts used in our example but you will want to read those pages thoroughly at a later stage.

Download Example C Code

Example of kdb C DLL Compilation

Compiling a C DLL on Windows

The steps to compile and load a C DLL are as follows:

  1. Download kdb C example code. It contains the following files:
    1. The latest version of header files provided by kx. k.h and and q.lib for windows 64.
    2. mymoving.c - C code that defines our actual functions
    3. mymoving.def - Declares the functions we will be providing
  2. Compile the code using comprun.bat this requires MS Visual Studio 2010 installed
    1. It uses "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64
      to set the required environment variables for our compilation.
    2. Runs the command line compiler: cl /LD /DKXVER=3 mymoving.c mymoving.def q.lib
    3. Launch q running the script load-functions.q so that our DLL functions are loaded and defined.
  3. Now within q we can call our mytop, myavg functions.



Loading a module


In this q script we import functions from our DLL and assign them to variable names:
mymoving is the name of our DLL which is in the current directory, you could place this in c:/q/w64 or specify a full path.
2: is the kdb function for loading native code.
The right hand side e.g. (`mysum;2) specifies a function that exists and the number of arguments it accepts.

Executing our C Code

As you can see we get the same results using either our own function or kdb's builtin mavg. However our function is 46x quicker (users on linux report a much smaller 3x speed difference). There are a number of reasons why this is (nulls and partitioned data) but that's a topic for another post.

If your having problems compiling or want advice either contact us or ask in our forums. We also provide kdb consulting services and have particular expertise in writing kdb+tick subscribers and feedhandlers.