Call a foreign-language function: Difference between revisions

Content added Content deleted
(Added Odin variant)
Line 2,057: Line 2,057:
(g_application_run app 0 #false)
(g_application_run app 0 #false)
</syntaxhighlight>
</syntaxhighlight>

=={{header|Odin}}==

<syntaxhighlight lang="odin">package main

import "core:fmt"

foreign import libc "system:c"

@(default_calling_convention="c")
foreign libc {
@(link_name="strdup") cstrdup :: proc(_: cstring) -> cstring ---
@(link_name="free") cfree :: proc(_: rawptr) ---
}

main :: proc() {
s1 : cstring = "hello"
s2 := cstrdup(s1)
fmt.printf("{}\n", s2)
cfree(rawptr(s2))</syntaxhighlight>

=={{header|Oz}}==
=={{header|Oz}}==
First we need to create a so-called "native functor" that converts the arguments and describes the C functions:
First we need to create a so-called "native functor" that converts the arguments and describes the C functions: