Hi, I never had time to really test this great language, in consequence I'm a kind of eternal noob, so I apologize for the trivial problem.

I try to bind a minimal C function for learning purpose.

c_function.h

int addTwo(int x)
  {
    return x + 2;
  }

c_function.nim

proc addTwo*(x:int):int {.header: "c_function.h", importc: "addTwo".}

use_c_function.nim

import c_function

echo addTwo(1)
Nim's debugger throws an error from GCC because the file "c_function.h" is not found. All the source files above are in the same location. What is the proper way please ?

2017-12-04 22:30:13

You should tell nim where to look. There is an option --cincludes (try nim --advanced).

So in your case: nim c --cincludes:"." use_c_function.nim

2017-12-05 04:09:08
Trying to compile use_c_function.nim results in
Error: execution of an external compiler program 'gcc -c  -w  -I/home/ftep/.choosenim/toolchains/nim-0.17.2/lib -o /tmp/cftest/nimcache/use_c_function.o /tmp/cftest/nimcache/use_c_function.c' failed with exit code: 1
which points to the problem: the include path defined by the -I compiler option does not contain c_function.h. This is fixed by
nim c --passC='-I.' -r use_c_function.nim
2017-12-05 09:02:30
Thanks for the tip. I guess in a serious project the headers are linked using a build tool. 2017-12-05 17:13:36