Ocamlmklib vs just including .o files

What are the practical differences between the following?

Option 1: Just include the .o files in the cmxa file with a call to ocamlopt

all: main

stub.o: stub.c
        gcc -I$$(opam var lib)/ocaml -c stub.c

test.cmx: test.ml
        ocamlopt -c test.ml

test.cmxa: test.cmx stub.o
        ocamlopt -cclib -levent -a stub.o test.cmx -o test.cmxa

main.cmx: test.ml
        ocamlopt -c main.ml

main: test.cmxa main.cmx
        ocamlopt test.cmxa main.cmx -o main

.PHONY: clean
clean:
        rm -f *.cmx *.cmi *.cmxa *.o main

Option 2: Build a library out of the .o files and link to them (using ocamlmklib)

all: main

stub.o: stub.c
        gcc -I$$(opam var lib)/ocaml -c stub.c

test.cmx: test.ml
        ocamlopt -c test.ml

test.cmxa: test.cmx stub.o
        ocamlmklib -cclib -levent test.cmx stub.o -o test

main.cmx: main.ml
        ocamlopt -c main.ml

main: test.cmxa main.cmx
        ocamlopt -I ./ test.cmxa main.cmx -o main

.PHONY: clean
clean:
        rm -f *.cmx *.cmi *.cmxa *.o *.so *.a main

My assumption is option 1 is required for bytecode support. If all I care about is native support is there any practical difference?

Thanks

The ocamlmklib is wrapper about various call to gcc, ar and ocamlc or ocamlopt. The line

ocamlmklib -cclib -levent test.cmx stub.o -o test

is equivalent (using -verbose) to

gcc -shared    -o ./dlltest.so stub.o     
ar rc ./libtest.a  stub.o
ocamlopt -a   -o test.cmxa  test.cmx -cclib -ltest  -cclib -levent

Thus your option 2 is doing strictly more work than your option 1 and neither are supporting bytecode executable.

Right. I guess my question is what is the point of that doing extra work in the context of Ocaml libraries:

In Option 1:

  • test.cmxa refers to stub.o
  • stub.o is installed

In Option 2:

  • text.cmxa refers to -ltest
  • libtest.a and dlltest.so are both created and installed

The final executable main’s are identical