Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template/generic instantiation of importc from here #118

Open
blockoutdev opened this issue Aug 21, 2024 · 8 comments
Open

template/generic instantiation of importc from here #118

blockoutdev opened this issue Aug 21, 2024 · 8 comments

Comments

@blockoutdev
Copy link

blockoutdev commented Aug 21, 2024

trying to generate a wrapper for flecs (https://github.com/SanderMertens/flecs)

directory looks like this

root /
   wrap_flecs.nim
   flecs /
      flecs.c
      flecs.h
      libflecs.so
      libflecs_static.a
   

wrap_flecs.nim is just

import futhark

{.passL: "./flecs/libflecs_static.a".}

importc:
  outputPath "./libflecs_generated.nim"
  path "./flecs" 
  "flecs.h"

nim has an issue with importc line, its giving the error in the title.

@PMunch
Copy link
Owner

PMunch commented Aug 21, 2024

Do you have a full error? That is only a small part of the error messages.

@blockoutdev
Copy link
Author

the editor is also pointing out the issue but it only shows "template/generic instantiation of importc from here"

stack trace: (most recent call last)
/home/alex/.nimble/pkgs2/futhark-0.13.4-0bfc8e592b6707dd725093236736b06956840cd7/futhark.nim(952, 21) importcImpl
/home/alex/.nimble/pkgs2/futhark-0.13.4-0bfc8e592b6707dd725093236736b06956840cd7/futhark.nim(454, 41) createStruct
/home/alex/.nimble/pkgs2/futhark-0.13.4-0bfc8e592b6707dd725093236736b06956840cd7/futhark.nim(214, 50) sanitizeName
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/strutils.nim(2199, 26) replace
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/strutils.nim(1891, 16) initSkipTable
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/strutils.nim(1879, 7) initSkipTable
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/algorithm.nim(98, 15) fill
/home/alex/VSCode/NimProjects/testNim/wrap_flecs.nim(5, 1) template/generic instantiation of `importc` from here
/home/alex/.nimble/pkgs2/futhark-0.13.4-0bfc8e592b6707dd725093236736b06956840cd7/futhark.nim(708, 14) template/generic instantiation of `importcImpl` from here
/home/alex/.choosenim/toolchains/nim-2.0.8/lib/pure/algorithm.nim(98, 15) Error: interpretation requires too many iterations; if you are sure this is not a bug in your code, compile with `--maxLoopIterationsVM:number` (current value: 10000000)

@PMunch
Copy link
Owner

PMunch commented Aug 21, 2024

Yeah, the editor shortens the message, sometimes to the point where it is useless..

The actual problem here is at the end:

Error: interpretation requires too many iterations; if you are sure this is not a bug in your code, compile with `--maxLoopIterationsVM:number` (current value: 10000000)

This happens if the output from Opir is large, which happens very easily. Just throw a couple of zeros on the end of that number and it works fine.

@blockoutdev
Copy link
Author

blockoutdev commented Aug 21, 2024

Thanks for the help. The nim wrapper code is being generated now. I put together a simple test to see if it works, but the compiler is giving "undefined reference" errors for all the symbols im using from the library. im compiling with just nim c test_flecs_wrapper.nim

honestly im not sure if Im grasping this fully. once the nim wrapper code is generated, do I need to reference the nim file that generated the wrapper? and is an include or import statement used if I want to use the wrapper?

the editor is picking up the symbols fine and suggesting autocompletes

test_flecs_wrapper:


import libflecs
var world = ecs_init()

var e = ecs_new(world)
echo ecs_is_alive(world, e)
ecs_delete(world, e)
echo ecs_is_alive(world, e)

discard ecs_fini(world) 

error message:

Hint: used config file '/home/alex/.choosenim/toolchains/nim-2.0.8/config/nim.cfg' [Conf]
Hint: used config file '/home/alex/.choosenim/toolchains/nim-2.0.8/config/config.nims' [Conf]
...................................................................................................................
/home/alex/VSCode/NimProjects/testNim/libflecs.nim(4815, 16) Warning: Declaration of FILE exists but with different size [User]
/home/alex/VSCode/NimProjects/testNim/libflecs.nim(7603, 9) Hint: Declaration of FILE already exists, not redeclaring [User]
.
/home/alex/VSCode/NimProjects/testNim/libflecs.nim(4815, 16) Warning: Declaration of FILE exists but with different size [User]
/home/alex/VSCode/NimProjects/testNim/libflecs.nim(7603, 9) Hint: Declaration of FILE already exists, not redeclaring [User]
CC: libflecs.nim
CC: test_flecs_wrapper.nim
Hint:  [Link]
/usr/bin/ld: /home/alex/.cache/nim/test_flecs_wrapper_d/@mtest_flecs_wrapper.nim.c.o: in function `NimMainModule':
@mtest_flecs_wrapper.nim.c:(.text+0x1d7): undefined reference to `ecs_init'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x1f5): undefined reference to `ecs_new'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x224): undefined reference to `ecs_is_alive'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x282): undefined reference to `ecs_delete'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x2aa): undefined reference to `ecs_is_alive'
/usr/bin/ld: @mtest_flecs_wrapper.nim.c:(.text+0x308): undefined reference to `ecs_fini'

beneath the above messages the whole gcc command is printed out, but I excluded it for readability. ill put it down below.

@blockoutdev
Copy link
Author

collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc   -o /home/alex/VSCode/NimProjects/testNim/test_flecs_wrapper  /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@ssystem@sansi_c.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@ssystem@scoro_detection.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@sprivate@sbitops_utils.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@ssystem@scountbits_impl.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@ssystem@srepr_v2.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@sstd@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@m..@s..@[email protected]@[email protected]@slib@[email protected] /home/alex/.cache/nim/test_flecs_wrapper_d/@mlibflecs.nim.c.o /home/alex/.cache/nim/test_flecs_wrapper_d/@mtest_flecs_wrapper.nim.c.o  -pthread -pthread -lm -lm -lrt   -ldl'

@blockoutdev
Copy link
Author

libflecs.nim.txt

heres the generated code for reference

@PMunch
Copy link
Owner

PMunch commented Aug 23, 2024

You just import the module containing the Futhark code, it will cache the output so once you've run it once it's basically a no-op unless you change anything.

Those errors are from the C compiler, Futhark doesn't attempt to tell the C compiler how to find the symbols because it can be done in multiple different ways. Typically you'd have a so/dll/dynlib file, or you link against a .a file, or even you just include the .h files. But that all depends on your library.

@blockoutdev
Copy link
Author

wow, it works. I have it running on compilation and it echos everything out as expected. Whats the best way to get the editor to recognize the symbols? I had trouble trying to import the generated nim code directly (the result of passing outputPath to importc). am I better off refactoring the wrapper with my own nim code to obscure all the 'undeclared reference' issues, or is there a way to get editor integration with the futhark code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants