So I'm working on wrapper for a library (stb_image) that is contained in a single header file. Right now if I want to get things to compile and work, I have to tell the nim compiler where that header file is located. For example, if my project structure is like this:
src/
- main.nim
- header_lib.h
- header_lib.nim
And I'm compiling from that src/ directory, I have to do this: nim c main.nim --cincludes:. I'd really like to drop that --includes part, so whoever uses this wrapper only has to do the import statement.
Is there a way that I can do this without copy and pasting the source of the header into the .nim wrapper file?
I think you could rename your header file to c and then inside header_lib.nim add:
{.compile: "header_lib.c".}
http://nim-lang.org/docs/manual.html#implementation-specific-pragmas-compile-pragma. There are also ways to pass arguments to the compiler from code http://nim-lang.org/docs/manual.html#implementation-specific-pragmas-passc-pragmaResurrecting this thread as I stumbled upon the exact same issue.
The library from def_pri_pub (stb_image) and the library I'm wrapping (ttmath) are header-only so we can't link to a library.
I've noticed that {.importc: "someProc", header: "path/to/header".} is either absolute or relative to the nimcache directory.
I think header: should either:
This workaround worked for me, but it is still hacky. Assuming the header file is in the same directory than the nim file:
from os import splitPath
{.passC:"-I" & currentSourcePath().splitPath.head .}
Not tested, but the following could work.
const myHeader = currentSourcePath().splitPath.head & "/myHeader.h"
...
proc foo() {.importc, header: myHeader.}
I tried to import a C file, that includes GLFW, in "test.nim", like this but I fail.
test.c
#include <stdio.h>
#include "test.h"
// Just for quick testing.
void version()
{
printf("%s\n", glfwGetVersionString());
}
test.h
#ifndef TEST
#define TEST
#include <GLFW/glfw3.h>
void version();
#endif
test.nim
{.link: "test.o".}
proc version() {.importc: "version".}
version()
And finally the build commands (all files are in the same folder)
all: test.o
nim c --passC:'`pkg-config glfw3 --static --cflags --libs`' test.nim
test.o: test.c
gcc -Wall -c test.c
All GLFW symbols in the C file are "undefined references". I tried to build in full C (with à "main()"), this "pkg-config" works fine. Did I miss something ?