//header.h
#ifndef HEADER_H
#define HEADER_H
#include <cstdio>
extern "C" {
void foo() { printf("foo\n"); }
}
#header.nim
proc foo*(){.header:"header.h",importc.}
proc bar*() = foo()
#code.nim
proc main() =
foo()
bar()
main()
result of nim cpp code.nim:
/usr/bin/ld: /home/bwsq/.cache/nim/code_d/@mcode.nim.cpp.o: in function `foo':
@mcode.nim.cpp:(.text+0x0): multiple definition of `foo'; /home/bwsq/.cache/nim/code_d/@mheader.nim.cpp.o:@mheader.nim.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
This is maybe a C++ question, sorry, but is there some magic combination of extern/static/inline that I haven't tried yet to make this work?
Here's a workaround:
#header.nim
proc crud(){.header:"header.h", importc: "foo" .}
proc foo*() = crud()
proc bar*() = foo()
Not a Nim problem, doesn't work in C++ either.
Solution is all exported functions in header.h need to be declared inline