I'm trying to write a Nim library to use in C code. These are my files:
test.nim:
import strutils
proc NimMain() {.importc.}
proc test(p, s: cstring): cint {.cdecl, exportc.} =
NimMain()
if $p in ($s).split(','):
return 1
return 0
main.c:
#include <stdio.h>
extern int test( char const *p, char const *s);
int main()
{
for (int i = 0;i < 10000; i++){
printf("%d\n", i);
printf(">>>> %d\n", test("aaa", "aaa,bbb,vvvv"));
printf(">>>> %d\n", test("zzzaaa", "aaa,bbb,vvvv"));
}
return 0;
}
Makefile:
main: libtest.a
gcc -g -o main main.c libtest.a -Inimcache
libtest.a:test.nim
nim c --app:staticlib --noMain -d:debug test.nim
clean:
rm -rf nimcache
rm -f main
rm *.a
After 1750 iteration I get this error: [GC] cannot register thread local variable; too many thread local variables.
What am I doing wrong?
Some details:
$ nim --version
Nim Compiler Version 1.6.0 [Linux: amd64]
Compiled at 2021-10-19
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 727c6378d2464090564dbcd9bc8b9ac648467e38
active boot switches: -d:release
You should not call NimMain more than once. You can use system.once like this:
import strutils
proc NimMain() {.importc.}
proc test(p, s: cstring): cint {.cdecl, exportc.} =
once: NimMain()
if $p in ($s).split(','):
return 1
return 0
Or simply call NimMain independently in your C code.