How to compile OCaml program on Linux for running on FreeBSD

I have written a simple web application using OCaml and the Opium library. Is it possible to use either ocamlc or ocamlopt to compile the program on Ubuntu Linux in such a way that the program will run on FreeBSD? Both the Ubuntu Linux machine and the FreeBSD machine have x86-64 processors.

If your application is pure OCaml code (no C bindings), then the bytecode generated by ocamlc will happily run anywhere, as long as you have an OCaml interpreter (ocamlrun) in your target system.

Otherwise, I don’t think this is currently possible for native-code.

Cheers,
Nicolas

Chapter 11. Linux Binary Compatibility | FreeBSD Documentation Portal might help.

I’ll mention that the (Linux x86-64 ╳ FreeBSD x86-64) cross doesn’t seem on the surface to present much difficulties. If someone you know understands how to setup a cross-compiler from Linux to FreeBSD in C (preferably with clang and an easy-to-download FreeBSD sysroot), and would be willing to maintain the cross, I can walk them through adding it to dkml-base-compiler.4.14.x. That compiler package supports, among other things, Linux/Darwin x86-64 to Android x86-64. And the first step for FreeBSD … registering it in dkml-c-probe … is already done.

Note: I lost some links to CI jobs that were examples of how to use dkml-base-compiler to cross-compile Android. The next best thing is looking at the more complicated DkHelloWorld CI pipeline and the tail end of its logs to see what dkml-base-compiler can do. You’ll see Android clang cross-compiles, macOS x86-64 to arm64 clang cross-compiles, Linux GCC compilation using dockcross, and MSVC. Search for 1/5 Test #1: DkHelloWorld. GIVEN main-cli WHEN inspect with 'file' THEN get architecture and the line above that will show the native code architecture that was compiled.

3 Likes