I first tried without the extra_objects stanza, then the build of the library works fine, but it fails when linking the executable with undefined reference to 'ASM_Succ'. When looking at the dune documentation, it seems like the extra_objects stanza is the solution to the problem. But then i get the following error.
/usr/bin/ld: lib/test.o: in function `ASM_Succ':
test.asm:(.text+0x0): multiple definition of `ASM_Succ'; lib/libc_test_stubs.a(test.o):test.asm:(.text+0x0): first defined here
It seems like dune is adding test.o into lib/libc_test_stubs.a and also using test.o directly making my function defined twice. If i remove test.o from libc_test_stubs.a the command that dune runs succeeds.
Am i doing something wrong, or is dune misbehaving? Is there some other stanza i should be using to get this to work?
#include "caml/mlvalues.h"
#include "caml/memory.h"
int ASM_Succ(int, int);
CAMLprim value c_succ(value val, value rep) {
CAMLparam2(val,rep);
int v = Int_val(val);
int r = Int_val(rep);
int ret = ASM_Succ(v,r);
CAMLreturn (Val_int(ret));
}
test.asm
bits 64
global ASM_Succ
section .text
ASM_Succ:
inc rdi
dec rsi
jnz ASM_Succ
mov rax, rdi
ret
test.ml
external succ: int -> int -> int = "c_succ"
So yes, i was missing an extern. but adding it doesn’t seem to help. I still get the same error.
Today i found that I can successfully run the asm code if i move the .asm file to the bin folder and move the dune rule to build the asm file and the extra_objects stanza to the executable stanza in the bin folder dune file.
However, this doesn’t seem like a solution to the problem. I would like to be able to keep it in the lib folder. According to the dune docs extra_objects should work for libraries as well.