OCaml Compilation Error compiling Lablgl and Lablgtk - opengl

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

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

Trouble building GLEW on Windows using MinGW

I'm trying to build GLEW 2.1.0 on Windows (8.1) using MinGW and have been struggling for a while now.
I tried to follow the instructions for "MSYS/Mingw" on the GLEW GitHub page but when running mingw32-make, I only got a couple errors about missing commands (test and config) as well as the following: Makefile:40: *** "Platform '' not supported". Stop.
Under "Requirements" it says that bash is needed, so I installed git, because that apparently comes with a version of bash. Running mingw32-make (in the directory that all the GLEW files and folders are in) using Git Bash results in a long list of warnings and "undefined reference"s. The latter come from the linker as far as I know, but I have no idea what could cause them. As it's so much text, I'll only include the last few lines here:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: tmp/mingw/default/shared/glewinfo.o:glewinfo.c:(.text.startup+0x181b): undefined reference to `_imp___iob'
collect2.exe: error: ld returned 1 exit status
Makefile:181: recipe for target 'bin/glewinfo.exe' failed
mingw32-make: *** [bin/glewinfo.exe] Error 1
mingw32-make install has the following output:
$ mingw32-make install
install -d -m 0755 "/usr/include/GL"
install -m 0644 include/GL/wglew.h "/usr/include/GL/"
install -m 0644 include/GL/glew.h "/usr/include/GL/"
install -m 0644 include/GL/glxew.h "/usr/include/GL/"
sed \
-e "s|#prefix#|/usr|g" \
-e "s|#libdir#|/usr/lib|g" \
-e "s|#exec_prefix#|/usr/bin|g" \
-e "s|#includedir#|/usr/include/GL|g" \
-e "s|#version#|2.1.0|g" \
-e "s|#cflags#||g" \
-e "s|#libname#|glew32|g" \
-e "s|#requireslib#|glu|g" \
< glew.pc.in > glew.pc
install -d -m 0755 "/usr/lib"
install -d -m 0755 "/usr/bin"
install -m 0755 lib/glew32.dll "/usr/bin/"
install -m 0644 lib/libglew32.dll.a "/usr/lib/"
install -m 0644 lib/libglew32.a "/usr/lib/"
install -d -m 0755 "/usr/lib/pkgconfig"
install -d -m 0755 "/usr/lib/pkgconfig"
install -m 0644 glew.pc "/usr/lib/pkgconfig/"
And mingw32-make install.all produces another long list of undefined references and the same error that came from running mingw32-make. The first lines however are (about) the same as the result of running mingw32-make install.
As expected with all of the errors, running these commands didn't produce a binary file. I guess it could have something to do with me not having make, which is one of the requirements listed on the GitHub page. However I don't understand how I should install make on Windows and why it is even needed when the instructions say to use mingw32-make.
I've also tried some of the suggestions made in this thread, but that doesn't seem to have worked either.
What could be the problem and what can I do to successfully compile GLEW?
I could never build it with the instructions I found around the internet, so here's how I build glew (using https://downloads.sourceforge.net/project/glew/glew/2.2.0/glew-2.2.0.tgz) under MSYS2 with MinGW-w64:
# change the line below to your desired install path
INSTALLPREFIX=/usr/local
VERSION=2.2.0
gcc -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.o -c src/glew.c &&
gcc -fno-builtin -fno-stack-protector -shared -s -Wl,-soname,libglew32.dll -Wl,--out-implib,lib/libglew32.dll.a -o lib/glew32.dll src/glew.o -Wl,--as-needed -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 -nostdlib &&
ar cr lib/libglew32.a src/glew.o &&
gcc -DGLEW_MX -O2 -Wall -W -Iinclude -DGLEW_BUILD -o src/glew.mx.o -c src/glew.c &&
gcc -fno-builtin -fno-stack-protector -shared -s -Wl,-soname,libglew32mx.dll -Wl,--out-implib,lib/libglew32mx.dll.a -o lib/glew32mx.dll src/glew.mx.o -Wl,--as-needed -lglu32 -lopengl32 -lgdi32 -luser32 -lkernel32 -nostdlib &&
ar cr lib/libglew32mx.a src/glew.mx.o &&
gcc -O2 -c -Iinclude -o src/glewinfo.o src/glewinfo.c &&
gcc -s -o src/glewinfo.exe src/glewinfo.o lib/libglew32.dll.a -Wl,--as-needed -lgdi32 -lopengl32 &&
gcc -O2 -c -Iinclude -o src/visualinfo.o src/visualinfo.c &&
gcc -s -o src/visualinfo.exe src/visualinfo.o lib/libglew32.dll.a -Wl,--as-needed -lgdi32 -lopengl32 -lglu32 &&
sed -e "s?#prefix#?$INSTALLPREFIX?; s?#libdir#?\$\{prefix\}/lib?; s?#version#?$VERSION?; s?#requireslib#?glu?; s?#cflags#??; s?#libname#?glew32?" glew.pc.in > glew.pc &&
sed -e "s?#prefix#?$INSTALLPREFIX?; s?#libdir#?\$\{prefix\}/lib?; s?#version#?$VERSION?; s?#requireslib#?glu?; s?#cflags#?-DGLEW_MX?; s?#libname#?glew32mx?" glew.pc.in > glewmx.pc &&
echo Installing... &&
mkdir -p $INSTALLPREFIX/include $INSTALLPREFIX/lib/pkgconfig $INSTALLPREFIX/bin $INSTALLPREFIX/cmake &&
cp -rf include/GL $INSTALLPREFIX/include/ &&
cp -f lib/*.a $INSTALLPREFIX/lib/ &&
cp -f *.pc $INSTALLPREFIX/lib/pkgconfig/ &&
cp -f lib/*.dll src/*.exe $INSTALLPREFIX/bin/ &&
cp -f build/cmake/*.cmake $INSTALLPREFIX/cmake/ &&
echo Success

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.

Build native executable with profile information using Ocamlbuild

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