Examples of a C program calling K? (including how to use gcc to

When interfacing between K and C, there are two scenarios:(1) a C program could call K(2) a K program could call CI’m interested in scenario (1).Scenario (1) is described in the Cookbook at code.kx.com here:https://code.kx.com/trac/wiki/Cookbook/InterfacingWithCIt says to use the C header file here:https://code.kx.com/trac/browser/kx/kdb%2B/c/c/k.hHowever, it doesn’t give details on how to compile and link using gcc.I don’t understand how a C program can call K. K is an executable, nota shared library - so how can a C program call a function in K?Does anyone have an example of a C program calling K - including howto link and compile on Linux using gcc?Thanks.Note A:There is another Cookbook page, which describes scenario (2):https://code.kx.com/trac/wiki/Cookbook/ExtendingWithCThis page does show how to use gcc to compile and link a C librarywhich K can then call.But this is easy because you’re calling a C library you wroteyourself, so you can compile it as a shared library, rather than as astand-alone executable.Scenario (1) seems much harder, because a C program would be calling K- and K isn’t a shared library that can be linked and called fromanother program, it’s a stand-alone executable.Note B:There’s also further mentions about a C program calling K on someolder pages:http://www.kx.com/a/k/connect/c/CandK.txt(See the section towards the end, “Accessing a Kdb Server with KDBC”.)http://kx.com/a/k/connect/c/k20.hBut again, there doesn’t seem to be any mention of how to compile andlink using gcc.Thanks for any help.

By the way, I forgot to mention, I’m using mostly Linux.

You can’t embed the q interpreter in your code, but you can call out across tcp/ip to a kdb+ instance.

The 2 scenarios you talk of, are really

(1) c code calls k code

(2) k code calls c code

which are concisely shown here

http://kx.com/q/c/c/a.c

For calling q/k across the wire, have a look at

http://kx.com/q/c/c/c.c

thanks

Thanks!The first link you provided:http://kx.com/q/c/c/a.cdoesn’t involve C code calling K code (at least not in the firststep). The first step involves K code calling C code in a sharedlibrary (where the :2 dyad has been used to inform K of the locationof that shared library), and then in turn the C code calls K again.So the K code initiated the process here, calling C code (which thencalled K code).I’m interested in a more typical use-case where, say, a web serverwants to query a database. In this situation, the C code (in the webserver) initiates the process, calling K code (the database). Itwouldn’t make sense for the K code to initiate this process - it wouldbe initiated by the C code, when the user makes a query.Regarding the second link:http://kx.com/q/c/c/c.cThis does appear to deal with the scenario I was talking about: C codeinitially calling K code. I also see the sample gcc command in thecomment at the top.What I’m worried about is this: The C program c.c includes k.h, whichis provided here:http://kx.com/q/c/c/k.hThis C header file k.h declares the following extern functions:extern I khpun(const S,I,const S,I), khpu(const S,I,const S),khp(const S,I),ymd(I,I,I),dj(I);extern V r0(K), sd0(I), m9(), kclose(I);extern S sn(S,I), ss(S);extern K ktj(I,J), ka(I), kb(I), kg(I), kh(I), ki(I), kj(J), ke(F),kf(F), kc(I), ks(S), kd(I), kz(F), kt(I), sd1(I,K(*)(I)), dl(V*f,I),ktn(I,I), knk(I,…), kp(S), kpn(S,I), ja(K*,V*), js(K*,S), jk(K*,K),k(I,const S,…), xT(K), xD(K,K), ktd(K), r1(K), krr(S), orr(S),dot(K,K), b9(I,K), d9(K);Where are these extern functions defined? I assume they’re defined inthe K executable.How can gcc link to an executable?Won’t gcc complain “undefined reference” when attempting to compile aC program which includes k.h?Don’t we need K in the form of a library (either shared *.so or static*.a) in order to be able to call it from C?Thanks.

link with 

http://kx.com/q/l64/c.o

or for 32bit

http://kx.com/q/l32/c.o

I don’t need to embed K in my C program - it’s fine as you said to
just call it “over the wire” (via TCP/IP).

If I understand correctly, many of the extern functions and typedefs
in k.h serve to serialize and deserialize data, to and from the format
used by K.

The c.c code here which you mentioned:

http://kx.com/q/c/c/c.c

#include"k.h"

int main(){
int c=khp(“localhost”,5001); //connect
k(-c,“trade insert(14:34:56.789;IBM;99.54;300)”,(K)0); //insert
k(c,“”,(K)0); // flush
return 0;}

is exactly what I’m trying to do: have C code calling K code.

I just can’t figure out how the functions khp and k are going to get
compiled by gcc. They are declared extern in k.h, which is included in
c.c - but where are they defined? They’re defined in the K executable.
How can their definitions be “seen” by the code in c.c?

And in the commented-out code further down in c.c, there’s lots of
other functions getting called (which are declared extern in k.h). How
can c.c “see” the definitions of these functions?

If the functions declared in k.h are defined in k (a stand-alone
executable), then merely including k.h in c.c doesn’t make those
functions defined in c.c, does it?

Maybe I’m missing something, but I can’t see how c.c can call the
functions declared in k.h without some sort of dynamic library (eg,
k.so) or shared library (eg, k.a) in which those functions are
defined.

Yes they’re defined in the k stand-alone executable, but including k.h
in c.c doesn’t make them defined in c.c, does it?

Thanks.

Aha! Yes, that’s what I was looking for! An *.o file!Thanks!On Jul 12, 5:27?am, Charles Skelton <char…> wrote:> link with>> http://kx.com/q/l64/c.o&gt;&gt; or for 32bit>> http://kx.com/q/l32/c.o&gt;&gt; On Tue, Jul 12, 2011 at 10:18 AM, Stefan Scott Alexander <>>>>>>>> stefanscottal...@gmail.com> wrote:> > Thanks!>> > The first link you provided:>> >http://kx.com/q/c/c/a.c&gt;&gt; > doesn’t involve C code calling K code (at least not in the first> > step). The first step involves K code calling C code in a shared> > library (where the :2 dyad has been used to inform K of the location> > of that shared library), and then in turn the C code calls K again.>> > So the K code initiated the process here, calling C code (which then> > called K code).>> > I’m interested in a more typical use-case where, say, a web server> > wants to query a database. In this situation, the C code (in the web> > server) initiates the process, calling K code (the database). It> > wouldn’t make sense for the K code to initiate this process - it would> > be initiated by the C code, when the user makes a query.>> > Regarding the second link:>> >http://kx.com/q/c/c/c.c&gt;&gt; > This does appear to deal with the scenario I was talking about: C code> > initially calling K code. I also see the sample gcc command in the> > comment at the top.>> > What I’m worried about is this: The C program c.c includes k.h, which> > is provided here:>> >http://kx.com/q/c/c/k.h&gt;&gt; > This C header file k.h declares the following extern functions:>> > extern I khpun(const S,I,const S,I), khpu(const S,I,const S),> > khp(const S,I),ymd(I,I,I),dj(I);>> > extern V r0(K), sd0(I), m9(), kclose(I);>> > extern S sn(S,I), ss(S);>> > extern K ktj(I,J), ka(I), kb(I), kg(I), kh(I), ki(I), kj(J), ke(F),> > kf(F), kc(I), ks(S), kd(I), kz(F), kt(I), sd1(I,K()(I)), dl(Vf,I),> > ktn(I,I), knk(I,…), kp(S), kpn(S,I), ja(K*,V*), js(K*,S), jk(K*,K),> > k(I,const S,…), xT(K), xD(K,K), ktd(K), r1(K), krr(S), orr(S),> > dot(K,K), b9(I,K), d9(K);>> > Where are these extern functions defined? I assume they’re defined in> > the K executable.>> > How can gcc link to an executable?>> > Won’t gcc complain “undefined reference” when attempting to compile a> > C program which includes k.h?>> > Don’t we need K in the form of a library (either shared *.so or static> > *.a) in order to be able to call it from C?>> > Thanks.>> > –> >

Submitted via Google Groups</char…>

Thanks. While I was posting this question, you answered it! The linksyou postedhttp://kx.com/q/l64/c.ohttp://kx.com/q/l32/c.oappear to be what I was looking for.