#foreign import without library part

1 messages · Page 1 of 1 (latest)

oblique ginkgo
#

I'm trying to write a Neovim module in Odin, it compiles as dynamicall-linked library and load by require(my_odin_neovim_configuration) in Lua.

The module has the following entry point:

@(export)
luaopen_my_odin_neovim_configuration :: proc "c" () -> c.int {
    fmt.printf("\n>>> Hello from Odin neovim plugin.")
    // nvim_api_test()
    return 1
}

Compiled and can be loaded in Neovim and the printf works fine (I redirect the stderr to a file can see the print content: nvim 2>debug.log.

cat debug.log

>>> Hello from Odin neovim plugin.

#
# This comes from the C version module
#cat debug.log

>>> My neovim configuration C version:)
>>> api_ptr: 0x484590

But now, I tried to call neovim C API, so I write a sample API binding like this:

//
// Ignore all types,struct migrate from C to Odin...
//

when ODIN_OS == .Linux || ODIN_OS == .FreeBSD ||  ODIN_OS == .Darwin {
    // foreign import nvim "system:nvim" // This won't work, as 'libnvim.so' doesn't exists
    // foreign import nvim "nvim" // This won't work, as 'libnvim.so' doesn't exists
    foreign import nvim "" // This won't work, as not allow for emtry string
} else when ODIN_OS == .Windows {
    foreign import nvim "system:nvim.dll"
}

@(default_calling_convention="c")
foreign nvim {
    nvim_buf_line_count :: proc "c" (buffer: Buffer, err: ^Error) -> Integer ---
}

But the problem is that it seems Odin foreign import forces you to define the libraray part, so the odin compiler does -l libXXX link flag for you. In this case, I believe my_odin_neovim_configuration.so doesn't need to link at compile time, as nvim_buf_line_count will exists when Neovim is running. The C version works fine, as you only declare a foreign function by the extern keyword without providing any linker flag. Does it possible in Odin?

winged oak
#

The equivalent to just declaring the procs extern without explicitly linking anything is to just make a foreign block without specifying a foreign import

@(default_calling_convention="c")
foreign {
    nvim_buf_line_count :: proc "c" (buffer: Buffer, err: ^Error) -> Integer ---
}

Though, that leaves handling the linking aspect of it up to you

gaunt shadow
#

If that doesn't work you may want to get symbols at runtime using dlopen + dlsym

#

Are there no c examples you can port?