Kona API Help

48 views
Skip to first unread message

willworkf...@gmail.com

unread,
Apr 6, 2025, 12:32:26 AM4/6/25
to Kona Users
Trying to produce the most basic example of calling a c function from K, and I keep getting errors. Compile step succeeds, not sure what I'm doing wrong. Not sure if I was supposed to include -lkona which I did try to do and told me to recompile with -fPIC though I already was doing that. Any help is greatly appreciated. 

on Linux:
// directory
./includes/kona.h
./k
./t.k
./t.o
./t.c

// test.c
#include "includes/kona.h"
K add(K x, K y) {R(gi(Ki(x)+Ki(y)));} // return K int having added 2 ints together

// test.k
add:`"./t.o" 2:(`add;2)
add[1;2]
\\

//compile step
 cc -g t.c -o t.o  -shared -fPIC

// run 
./k ./t.k
kona      \ for help. \\ to exit.

./k: symbol lookup error: ./t.o: undefined symbol: gi




Bakul Shah

unread,
Apr 6, 2025, 12:45:32 PM4/6/25
to kona...@googlegroups.com
There are two issues.

1. gi() is defined in src/kapi.c. So add that file to the list of c files for building k.
2. But that is still not enough as these symbols are not exported for the use of runtime loaded code. You need to add
-Wl,-export-dynamic
to LDFLAGS for the linux case.

Note that not all functions defined in src/ckapi.txt are implemented but most are.

You can apply the following patch (though the freebsd case still doesn't work).
kona.diff

willworkf...@gmail.com

unread,
Apr 6, 2025, 4:05:05 PM4/6/25
to Kona Users
Thanks Bakul! 

The below repo illustrates the changes, and how to use the API at a simple level. https://github.com/carkat/konalib

willworkf...@gmail.com

unread,
Apr 6, 2025, 6:44:45 PM4/6/25
to Kona Users
Follow up questions:
I know I asked about linux because that's where I was starting. I would like to also compile for windows now that I can see that the API is working.
1. does your answer imply that additional flags are not necessary for the windows compilation steps? or are there additional edits needed to compile for windows?
2. which exact terminal am I to use to properly make the windows builds? I can't seem to download the right mingw/cygwin compilers to get a successful execution of make
3. is anyone compiling kona using zig cc or any native windows, like with wget in powershell?

willworkf...@gmail.com

unread,
Apr 6, 2025, 6:49:38 PM4/6/25
to Kona Users
sorry, I meant winget in powershell, not wget

Bakul Shah

unread,
Apr 6, 2025, 6:53:00 PM4/6/25
to kona...@googlegroups.com
May be Tom has an answer? I avoid Windows!

-- 
You received this message because you are subscribed to the Google Groups "Kona Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kona-user+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/kona-user/442097a4-323a-460e-9d5a-d70be7c2f60bn%40googlegroups.com.

willworkf...@gmail.com

unread,
Apr 6, 2025, 7:04:21 PM4/6/25
to Kona Users

Having only made the changes in Bakuls diff, the output I'm getting from "git bash" version of mingw64 make. This is the same error I'm getting when using then winget install make, and also the same error I'm getting from cygwin make. So I'm not really able to build kona for Windows with the current Makefile off of a fresh clone

OS="mingw64_nt-10.0-26100"
echo "#define KBUILD_DATE \"2025-04-06\"" >src/kbuild.h
cc -g -O3    -c -o src/main.o src/main.c
process_begin: CreateProcess(NULL, cc -g -O3 -c -o src/main.o src/main.c, ...) failed.

willworkf...@gmail.com

unread,
Apr 6, 2025, 7:16:31 PM4/6/25
to Kona Users
Update

I just got windows to compile with zig. But The question about which flags I might need to add for kapi to work is still open for anyone who knows how I might link on windows. I'll still keep poking

Thanks!

diff --git a/Makefile b/Makefile
index e58c586..65344c6 100644
--- a/Makefile
+++ b/Makefile
@@ -8,8 +8,8 @@ OS := $(shell uname -s | tr "[:upper:]" "[:lower:]")
 $(info OS="$(OS)")

 # Win-64
-ifeq (mingw64_nt-10.0-22000,$(OS))
-CC=gcc -DWIN32=1
+ifeq (mingw64_nt-10.0-26100,$(OS))
+CC=zig cc -target x86_64-windows #gcc -DWIN32=1
 PRODFLAGS += -D_FILE_OFFSET_BITS=64
 LDFLAGS = -lws2_32 -static -lpthread
 OBJS= src/win/mman.o src/win/dlfcn.o src/win/safe-ctype.o src/win/fnmatch.o \
@@ -154,7 +154,7 @@ TAGS: *.c *.h
 .PHONY: all clean install android-debug

 # Dependencies.
-ifeq (mingw64_nt-10.0,$(OS))
+ifeq (mingw64_nt-10.0-26100,$(OS))
 src/win/dlfcn.c: src/win/dlfcn.h
 src/win/mman.c: src/win/mman.h
 src/win/safe-ctype.c: src/win/safe-ctype.h

willworkf...@gmail.com

unread,
Apr 6, 2025, 7:31:13 PM4/6/25
to Kona Users

quick update. with the same diff as above, replacing CC with zig cc (which builds k.exe just fine) , trying to make kapi-test I get the following output, which looks exactly as I was getting before trying to run with the default makefile

For the sake of clarity, I'm not specifically trying to use zig cc, I'm just hacking trying to understand if the kapi will work on windows as well. Thanks all for your time and attention. I hope this can work for me. 

make kapi-test
OS="mingw64_nt-10.0-26100"
ar crv libkona.a src/win/mman.o src/win/dlfcn.o src/win/safe-ctype.o src/win/fnmatch.o src/win/pread.o src/win/usleep.o src/0.o src/bswap.o src/c.o src/getline.o src/mt.o src/p.o src/r.o src/k.o src/kc.o src/kx.o src/kg.o src/km.o src/kn.o src/ko.o src/ks.o src/v.o src/va.o src/vc.o src/vd.o src/vf.o src/vg.o src/vq.o src/kapi.o
process_begin: CreateProcess(NULL, ar crv libkona.a src/win/mman.o src/win/dlfcn.o src/win/safe-ctype.o src/win/fnmatch.o src/win/pread.o src/win/usleep.o src/0.o src/bswap.o src/c.o src/getline.o src/mt.o src/p.o src/r.o src/k.o src/kc.o src/kx.o src/kg.o src/km.o src/kn.o src/ko.o src/ks.o src/v.o src/va.o src/vc.o src/vd.o src/vf.o src/vg.o src/vq.o src/kapi.o, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [Makefile:113: libkona.a] Error 2

willworkf...@gmail.com

unread,
Apr 6, 2025, 8:11:50 PM4/6/25
to Kona Users
Update using zig cc and zig ar, now all steps are compiling. k works, but I don't know if the library works. Working on a simple example to test
Fun fact: zig cc compiles REALLY fast. 


This is now the full diff. 

$ git diff Makefile
diff --git a/Makefile b/Makefile
index e58c586..9d58d0b 100644
--- a/Makefile
+++ b/Makefile
@@ -8,8 +8,9 @@ OS := $(shell uname -s | tr "[:upper:]" "[:lower:]")

 $(info OS="$(OS)")

 # Win-64
-ifeq (mingw64_nt-10.0-22000,$(OS))
-CC=gcc -DWIN32=1
+ifeq (mingw64_nt-10.0-26100,$(OS))
+AR=zig ar

+CC=zig cc -target x86_64-windows #gcc -DWIN32=1
 PRODFLAGS += -D_FILE_OFFSET_BITS=64
 LDFLAGS = -lws2_32 -static -lpthread
 OBJS= src/win/mman.o src/win/dlfcn.o src/win/safe-ctype.o src/win/fnmatch.o \
@@ -47,16 +48,16 @@ ifeq (linux,$(OS))
 CFLAGS += -pthread
 OBJS= src/0.o src/bswap.o src/c.o src/getline.o src/mt.o src/p.o src/r.o \
       src/k.o src/kc.o src/kx.o src/kg.o src/km.o src/kn.o src/ko.o src/ks.o \
-      src/v.o src/va.o src/vc.o src/vd.o src/vf.o src/vg.o src/vq.o
-LDFLAGS = -lm -ldl
+      src/v.o src/va.o src/vc.o src/vd.o src/vf.o src/vg.o src/vq.o src/kapi.o
+LDFLAGS = -lm -ldl -Wl,-export-dynamic
 endif

 ifeq (freebsd,$(OS))
 CFLAGS += -pthread
-LDFLAGS = -lm
+LDFLAGS = -lm -Wl,-export-dynamic
 OBJS= src/0.o src/bswap.o src/c.o src/getline.o src/mt.o src/p.o src/r.o \
       src/k.o src/kc.o src/kx.o src/kg.o src/km.o src/kn.o src/ko.o src/ks.o \
-      src/v.o src/va.o src/vc.o src/vd.o src/vf.o src/vg.o src/vq.o
+      src/v.o src/va.o src/vc.o src/vd.o src/vf.o src/vg.o src/vq.o src/kapi.o
 endif

 ifeq (openbsd,$(OS))
@@ -154,7 +155,7 @@ TAGS: *.c *.h

 .PHONY: all clean install android-debug

 # Dependencies.
-ifeq (mingw64_nt-10.0,$(OS))
+ifeq (mingw64_nt-10.0-26100,$(OS))
 src/win/dlfcn.c: src/win/dlfcn.h
 src/win/mman.c: src/win/mman.h
 src/win/safe-ctype.c: src/win/safe-ctype.h

willworkf...@gmail.com

unread,
Apr 6, 2025, 8:49:33 PM4/6/25
to Kona Users
https://github.com/carkat/konalib/blob/windows-build/extern/edit-konamake // edited make file used to overwrite the kona makefile from git
https://github.com/carkat/konalib/blob/windows-build/extern/edit-kapi.c         // edits give from bakul
https://github.com/carkat/konalib/blob/windows-build/errors.txt                        // missing symbols after trying to build t.c 

Explanation:
windows-build branch of my repo has removed all extra code to produce a simple add[1;2] example of using the kapi from windows.
I think I've reached the limit of my ability to experiment any further. 

I installed zig by downloading from https://ziglang.org/download/ and placing the zig folder in my windows path. 
test if it works by opening mingw or powershell and typing 

zig

Should work if in the path before opening your terminal. 

What I tried:
zig cc and zig ar are assigned for my $(OS) version in the make file
I tried adding the -Wl,-export-dynamic to the LDFLAGS but when I try to compile t.c I get errors.txt


Thanks to everyone for you time and patience while I try to work through this.
Again, I'm NOT FIXATED ON ZIG, but I am just trying to push ahead with what I understand. Happy to try any suggestions. 

As always, any help is greatly appreciated.
Reply all
Reply to author
Forward
0 new messages