Hello,
I have been playing around with a hybrid app that uses C and Nim.
So far it has been fun to import stuff from Nim and use them in C.
I wanted someone to show me some ways to understand how sending memory between the two works.
I have a few ideas:
Approach n1 is risky and unsafe.
Could someone maybe show me an example of how to use approach n2?
I'm not too sure I understand GC_ref and GC_unref.
Thanks!
> So far it has been fun to import stuff from Nim and use them in C.
I recommend doing it ther other way - ergo calling C from Nim. It is much easier to accomplish.
Are you set on using seq ? You could use UncheckedArray instead it would be much easier for C interop.
I would like to create some custom functions in C using SDL2 and then import to Nim.
Do i need to compile a new SDL2 DLL with the new custom C functions?
I tried to create an example custom C function in its own header but i got this error when i compiled the nim that imports the function:
In file included from C:\dev\hybridv2/SDL2/include/SDL.h:32:0,
from C:\dev\hybridv2/main_proto.h:4,
from C:\Users\Administrator\nimcache\main_d\@mmain.nim.c:12:
C:\dev\hybridv2/SDL2/include/SDL_main.h:109:17: error: conflicting types for 'SDL_main'
#define main SDL_main
^
C:\Users\Administrator\nimcache\main_d\@mmain.nim.c:143:5: note: in expansion of macro 'main'
int main(int argc, char** args, char** env) {
^~~~
In file included from C:\dev\hybridv2/SDL2/include/SDL.h:32:0,
from C:\dev\hybridv2/main_proto.h:4,
from C:\Users\Administrator\nimcache\main_d\@mmain.nim.c:12:
C:\dev\hybridv2/SDL2/include/SDL_main.h:121:29: note: previous declaration of 'SDL_main' was here
extern SDLMAIN_DECLSPEC int SDL_main(int argc, char *argv[]);
^~~~~~~~
Error: execution of an external compiler program 'C:\Users\Administrator\nim-1.4.2\dist\mingw64\bin\gcc.exe -c -w -fmax-errors=3 -mno-ms-bitfields -IC:\Users\Administrator\nim-1.4.2\lib -IC:\dev\hybridv2 -o C:\Users\Administrator\nimcache\main_d\@mmain.nim.c.o C:\Users\Administrator\nimcache\main_d\@mmain.nim.c' failed with exit code: 1
The UncheckedArray[T] type is a special kind of array where its bounds are not checked. This is often useful to implement customized flexibly sized arrays. Additionally, an unchecked array is translated into a C array of undetermined size.
The base type of the unchecked array may not contain any GC'ed memory but this is currently not checked.
I'll try to answer your other questions :
> Do i need to compile a new SDL2 DLL with the new custom C functions?
That's one way to do it. You can also directly instruct Nim to compile your C file with the correct linker flags to link your DLL to it. Example :
const C_source = "source.c"
const MY_DLL_PATH = "./lib" # Replace with your path
const MY_DLL_HEADER_PATH = "./lib/include" # Replace with your path
const INCLUDE_FLAG = "-I" & MY_DLL_HEADER_PATH
const LINK_FLAG = ["-Wl,-rpath," & MY_DLL_PATH, "-lmylib.dll"] # Use correct names & path.
{.
compile: C_source,
passc: INCLUDE_FLAG,
passL: LINK_FLAG[0],
passL: LINK_FLAG[1],
.}
> I would like to create some custom functions in C using SDL2 and then import to Nim.
May I point you to already existing SDL2 wrapper for Nim instead so you can directly use Nim for your custom function ?
https://github.com/nim-lang/sdl2 https://github.com/Vladar4/sdl2_nim
As far as I know, both wrapper are up-to date (but I haven't used any)/
Thanks Clonk for the amazing answer!
I am aware of the nimble packages, I just want to extend them with some C code.
Thanks for that nice Nim snippet.