Build native executable with profile information using Ocamlbuild - ocaml

How to build with enabled profile information using Ocamlbuild? It seems -p doesn't work there.
Now, I use ocamlopt for this. For example,
$ ocamlfind ocamlopt -c -p -thread -package core test.ml
$ ocamlfind ocamlopt -p -o test -thread -package core -linkpkg test.cmx
How to do the same with
ocamlbuild -user-ocamlfind test.native

You can pass the argument explicitly,
ocamlbuild -cflags -p
or in your _tags file,
true : profile
There is also a rule for the file-name,
ocamlbuild test.p.native

Related

How to transform the Yojson Library of Ocaml into a .byte file using js_of_ocaml?

I am trying to transform the Yojson with the source code download online into a byte file using js_of_ocaml.
I try the following commands:
%: ocamlfind ocamlc -package yojson -linkpkg -g -o yojson.cmo
%: ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx -linkpkg -g -o -package yojson yojson.byte
I am trying to compile into bytecode an OCaml file but it give the following error:
%: ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx -linkpkg -o preprocessing.byte preprocessing.ml
File "preprocessing.ml", line 4, characters 5-22:
4 | open Yojson.Basic.Util
^^^^^^^^^^^^^^^^^
Error: Unbound module Yojson
The OCaml compiler will not use any compilation units that are not specified on the command line. In addition, you have to specify them in a correct order, so that a user of a unit will come after that unit, e.g.,
ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx -linkpkg -o preprocessing.byte yojson.cmo preprocessing.ml
Also, your invocation,
ocamlfind ocamlc -package yojson -linkpkg -g -o yojson.cmo
is incorrect and should issue an error. You need to specify the -c option to generate the object file.
With that said, you don't need to do what you're doing at all, like building yojson manually (you could install from opam). In fact, it is much better to use a high-level tool, like dune to build your js_of_ocaml programs. The dune documentation provides detailed instructions on how to do this

Required module 'Yojson' is unavailable without location

I am trying to build a simple program using Yojson: the main.ml file is just
let () = exit 1;
and an mod.ml file contains
open Yojson
open Yojson.Basic.Util
let rec json_to_tree json =
let _ = json |> member "key" in
()
I have a Makefile which first compiles .ml files, then links. For simplicity, let assume we run the three following commands:
ocamlfind ocamlc -package yojson -g -c mod.ml
ocamlfind ocamlc -package yojson -g -c main.ml
ocamlfind ocamlc -package yojson -g -o inter mod.cmo main.cmo
The problem is that the linking part produces the following error:
File "_none_", line 1:
Error: Required module `Yojson' is unavailable
which I have no idea to solve. Using the Unix module can lead to similar error which are resolved adding unix.cma when linking, but this doesn't help for Yojson.
For the sake of completeness, I installed yojson via opam, and ocamlfind list | grep yojson returns
yojson (version: 1.3.3)
yojson.biniou (version: 1.3.3)
This works.
ocamlfind ocamlc -package yojson -linkpkg -g -o inter mod.cmo main.cmo
While this works, https://github.com/ocaml/dune is the better option to building ocaml code.

Difference in program behavior with different make files

I have this OCaml program
open Core.Std;;
open Printf;;
let all l = List.fold ~f:(&&) ~init:true l;;
let any l = List.fold ~f:(||) ~init:false l;;
let main () = let bools = [true; false; true; true; false; true] in
printf "%b %b\n" (all bools) (any bools);;
main();;
And then two make files, the first is
all: a.out
#true
a.out: fold.cmx
ocamlfind ocamlopt -g -linkpkg -package core -package core_kernel -thread -w -10 fold.cmx
fold.cmx: fold.ml fold.cmi
ocamlfind ocamlopt -g -c fold.cmx -package core -package core_kernel -thread -w -10 fold.ml
fold.cmi: fold.mli
ocamlfind ocamlopt -g -c -package core -package core_kernel -thread -w -10 fold.mli
fold.mli: fold.ml
ocamlfind ocamlc -i -package core -package core_kernel -thread -w -10 fold.ml > fold.mli
clean:
#rm *.cmx *.cmi *.o tests 2>/dev/null || true
Which produces an a.out that gives the expected output of false true.
The second is
all: fold
#true
fold: fold.cmx
ocamlfind ocamlopt -g -o fold -linkpkg -package core -package core_kernel -thread -w -10 fold.cmx
fold.cmx: fold.ml fold.cmi
ocamlfind ocamlopt -g -c fold.cmx -package core -package core_kernel -thread -w -10 fold.ml
fold.cmi: fold.mli
ocamlfind ocamlopt -g -c -package core -package core_kernel -thread -w -10 fold.mli
fold.mli: fold.ml
ocamlfind ocamlc -i -package core -package core_kernel -thread -w -10 fold.ml > fold.mli
clean:
#rm *.cmx *.cmi *.o tests 2>/dev/null || true
which produces a fold that on my machine, hangs without output. The only difference between the two is that one of them puts its output in fold and the other puts it in a.out. The version numbers for my ocaml, ocamlc, ocamlopt, and ocamlfind are all 4.02.1 and opam show core says it's version 112.06.01. Any of you guys know what's causing the difference?
You are running the standard fold program. Try ./fold.

Include OCaml binary library

I'm trying to use the OcamlGraph library and it's not clear to me how to add it to my project. It was suggested to me that I just compile the source of the library along with the source of my project, but there has to be some way of linking the binary of a precompiled library without using OPAM. Can this be done, and if so, how?
Thanks.
compiling:
ocamlc -I /path/to/ocamlgraph -c mysource.ml
(as suggested by ocamlfind ocamlc -package ocamlgraph -c -only-show, if ocamlfind and ocamlgraph are installed)
linking:
ocamlc -I /path/to/ocamlgraph /path/to/ocamlgraph/graph.cma mysource.cmo -o test.byte
(as suggested by ocamlfind ocamlc -linkpkg -package ocamlgraph -only-show )
But normally, you should use ocamlfind and the ocamlgraph package of opam or your distribution.

OCaml Compilation Error compiling Lablgl and Lablgtk

I am trying to compile three files - toolbar.ml which uses lablgtk2, window.ml which uses lablgl and main.ml which calls both these files. Here is my shell script:
#!/bin/bash
echo "Compiling files"
eval $(opam config env)
echo "Compiling toolbar"
ocamlc -I +lablgtk2 lablgtk.cma gtkInit.cmo toolbar.ml
echo "Compiling window"
ocamlfind ocamlc window.ml -package lablgl.glut -linkpkg
ocamlc -c main.ml
ocamlc -o run window.cmo main.cmo toolbar.cmo
echo "Running executable"
./run
echo "Clearing up"
rm run main.cmo main.cmi main.o window.cmo window.cmi window.o a.out
rm toolbar.cmo toolbar.cmi
But I am getting this error message:
File "_none_", line 1:
Error: Error while linking window.cmo:
Reference to undefined global `Glut'
Does anyone know what I am doing wrong here?
Turns out I wasn't linking all the packages / creating a new package - this is how I fixed it:
#!/bin/bash
echo "Compiling files"
eval $(opam config env)
echo "Compiling toolbar"
ocamlc -a -o toolbar.cma -linkall -I +lablgtk2 lablgtk.cma gtkInit.cmo toolb ar.ml
echo "Compiling window"
ocamlfind ocamlc -a -o window.cma -linkall window.ml -package lablgl.glut -l inkpkg
ocamlc -c main.ml
ocamlc -o run window.cma toolbar.cma main.cmo
echo "Running executable"
./run
echo "Clearing up"
rm run main.cmo main.cmi main.o window.cmo window.cmi window.o a.out
rm toolbar.cmo toolbar.cmi
rm window.cma toolbar.cma