I have been getting an error when linking a project with external C code and was wondering if anyone could help me. I have come up with the simplest way to reproduce the error I can:
Here is my project structure:
main.ml
library
stuff.ml
stuff_external.c
and the actual file contents:
stuff.ml
external do_something : unit -> unit = "do_something"
stuff_external.c
#include <stdio.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
CAMLprim value do_something(value unit)
{
printf("Doing something\n");
return Val_unit;
}
main.ml
let _ =
Stuff.do_something ()
The idea is that I can compile the library in the nested folder, then reference the .cmxa
file in the above project while passing the -I
flag for compiling projects to look for the compiled files in the library folder.
My build process looks like this:
root/library> ocamlopt -c stuff_external.c stuff.ml
root/library> ocamlopt -a stuff_external.o stuff.cmx -o library.cmxa
root/library> cd ../
root> ocamlopt -I library -c main.ml
root> ocamlopt library/library.cmxa main.cmx -o program
I get an error in the final line being:
x86_64-linux-gnu-gcc: error: stuff_external.o: No such file or directory
File "caml_startup", line 1:
Error: Error during linking
make: *** [makefile:3: build] Error 2
There are a few things to note that might be helpful.
Building a project like this works fine for me when there are no external c files.
Furthermore, if I move the .o
file up to root
before the final line, the compilation works, so it is as if in the linking stage the -I
flag is only applying to OCaml files. I have however tried some of the other options for the c compiler flags but can’t get it to work.
Ultimately I would like for the project in the above directory to be able to find the .o
file.