# dl function in k.h

**URL:** <https://forum.kx.com/t/dl-function-in-k-h/11972>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 16, 2019, 11:16am UTC](https://forum.kx.com/t/dl-function-in-k-h/11972 "2019-11-16T11:16:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenkangcrack1](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@chenkangcrack1](https://forum.kx.com/u/chenkangcrack1)\
**Post date:** [November 16, 2019, 11:16am UTC](https://forum.kx.com/t/dl-function-in-k-h/11972/1 "2019-11-16T11:16:00Z")

</div>

Can anyone give a demo to me with the&nbsp; "dl" function.I'm not quite sure how to use this function.

### `dl`&nbsp; dynamic link

Signature:&nbsp;`K dl(V* f, I n)`

Function takes a C function that would take&nbsp;_n_&nbsp;K objects as arguments and returns a K object. Shared library only.

Returns a q function.

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [November 16, 2019, 3:12pm UTC](https://forum.kx.com/t/dl-function-in-k-h/11972/2 "2019-11-16T15:12:00Z")

</div>

on osx, putting resulting a.so in $QHOME/m64

$ more Makefile  
m64:  
&nbsp; &nbsp; &nbsp; &nbsp; gcc -bundle -undefined dynamic\_lookup a.c -o a.so

$ more a.c  
#define KXVER 3  
#include"k.h"  
static K myadd(K x,K y){  
&nbsp; if(xt!=-KJ||y-\>t!=xt)  
&nbsp; &nbsp; return krr(“type”);  
&nbsp; return kj(xj+y-\>j);  
}  
K f(K x){  
&nbsp; return dl(myadd,2);  
}

$ more a.q  
f:`a 2:(`f;1)  
f[3;4]

$ q a.q  
KDB+ 3.7t 2019.11.15…

7  
q)\

---

<div class="post-metadata">

**Author:** ![chenkangcrack1](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@chenkangcrack1](https://forum.kx.com/u/chenkangcrack1)\
**Post date:** [November 17, 2019, 7:55am UTC](https://forum.kx.com/t/dl-function-in-k-h/11972/3 "2019-11-17T07:55:00Z")

</div>

Why not just export myadd directly and call it in kdb? Why do you have to use “dl” function in the&nbsp; example?

? 2019?11?16??? UTC+8??11:13:29?Charles Skelton???

> on osx, putting resulting a.so in $QHOME/m64
> 
> $ more Makefile  
> m64:  
> &nbsp; &nbsp; &nbsp; &nbsp; gcc -bundle -undefined dynamic\_lookup a.c -o a.so
> 
> $ more a.c  
> #define KXVER 3  
> #include"k.h"  
> static K myadd(K x,K y){  
> &nbsp; if(xt!=-KJ||y-\>t!=xt)  
> &nbsp; &nbsp; return krr(“type”);  
> &nbsp; return kj(xj+y-\>j);  
> }  
> K f(K x){  
> &nbsp; return dl(myadd,2);  
> }
> 
> $ more a.q  
> f:`a 2:(`f;1)  
> f[3;4]
> 
> $ q a.q  
> KDB+ 3.7t 2019.11.15…
> 
> 7  
> q)\

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [November 17, 2019, 10:37am UTC](https://forum.kx.com/t/dl-function-in-k-h/11972/4 "2019-11-17T10:37:00Z")

</div>

yes, the most usual case is exactly as you say, to setup the mapping using 2: for each function.

Another example

https://github.com/KxSystems/embedPy/blob/master/py.c#L51

Using dl enables you to reduce symbol scope in the shared lib.

---

<div class="post-metadata">

**Author:** ![chenkangcrack1](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@chenkangcrack1](https://forum.kx.com/u/chenkangcrack1)\
**Post date:** [November 19, 2019, 2:39am UTC](https://forum.kx.com/t/dl-function-in-k-h/11972/5 "2019-11-19T02:39:00Z")

</div>

I’m so sorry I still can’t find the difference between

#define F(f,i) js(&n,ss(#f));jk(&v,dl(f,i));

&nbsp;F(eval,1)

and

&nbsp;#define&nbsp;F(f)&nbsp;js(&n,ss(#f));jk(&v,f);

&nbsp;F(eval)

? 2019?11?17??? UTC+8??6:37:46?Charles Skelton???

> yes, the most usual case is exactly as you say, to setup the mapping using 2: for each function.
> 
> Another example
> 
> https://github.com/KxSystems/embedPy/blob/master/py.c#L51
> 
> Using dl enables you to reduce symbol scope in the shared lib.

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [November 19, 2019, 10:33am UTC](https://forum.kx.com/t/dl-function-in-k-h/11972/6 "2019-11-19T10:33:00Z")

</div>

in the first, the actual code from embedpy, it is storing the k object returned from dl(). That k object contains the function pointer and the function rank, and has type 112. This can be passed around, reference counted, used for function invocation, and freed when no longer used.

in the second you are storing just the function pointer, no function rank, and is invalid anyway as type 0 lists **must** contain pointers to k objects.&nbsp;

---

<div class="post-metadata">

**Author:** ![chenkangcrack1](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@chenkangcrack1](https://forum.kx.com/u/chenkangcrack1)\
**Post date:** [November 20, 2019, 12:55am UTC](https://forum.kx.com/t/dl-function-in-k-h/11972/7 "2019-11-20T00:55:00Z")

</div>

OK, I see, thank you so much.

? 2019?11?19??? UTC+8??6:33:56?Charles Skelton???

> in the first, the actual code from embedpy, it is storing the k object returned from dl(). That k object contains the function pointer and the function rank, and has type 112. This can be passed around, reference counted, used for function invocation, and freed when no longer used.
> 
> in the second you are storing just the function pointer, no function rank, and is invalid anyway as type 0 lists **must** contain pointers to k objects.&nbsp;
