Use another language to call a function: Difference between revisions
Content deleted Content added
Added zkl |
Add Nimrod |
||
Line 590:
clean:
rm -f $(TARGET) *.o query.c</lang>
=={{header|Nimrod}}==
<lang nimrod>proc Query*(data: var array[1024, char], length: var cint): cint {.exportc.} =
const text = "Here am I"
if length > text.len:
for i in 0 .. <text.len:
data[i] = text[i]
length = cint(text.len)
return 1
return 0</lang>
Compile with <code>nimrod c --app:staticlib --no_main query.nim</code>
<lang c>#include <stdio.h>
extern int Query (char * Data, size_t * Length);
int main (int argc, char * argv [])
{
char Buffer [1024];
size_t Size = sizeof (Buffer);
if (0 == Query (Buffer, &Size))
{
printf ("failed to call Query\n");
}
else
{
char * Ptr = Buffer;
while (Size-- > 0) putchar (*Ptr++);
putchar ('\n');
}
}</lang>
Compile with <code>gcc -ldl -o main main.c ./libquery.nim.a</code> and execute the result <code>main</lang> binary:
<pre>./main
Here am I
</pre>
=={{header|OCaml}}==
|