How to separate binary code from source in ocaml? - ocaml

I have my source next to a build directory
ocamlc -c file1.ml
ocamlc -c file2.ml
…
ocamlc -o main.ml ….cmo
I have the source with the file generated by the compilation.
I would like an option of ocamlc that enable me to have this file in the build directory.
I know i can do ocamlc -o build/main.ml .....cmo but i would like to have to .cmo in the build directory too
Thank you for your help

The answer of your main question is very simple:
in order to put the main binary of your OCaml application in the build folder, you just need to run:
ocamlc -o build/main.exe main.ml ….cmo
Indeed as indicated in the documentation (man ocamlc):
-o exec-file
Specify the name of the output file produced by the linker. The
default output name is a.out, in keeping with the Unix tradition. […]
However, doing all the build steps by hand in the way you are suggesting in your question may not be very effective (in particular, all .cmo files will stay along with the sources in the same folder).
To avoid this and facilitate the build of your OCaml project, I suggest using a build management tool for OCaml, for example one of the four tools mentioned in the end of the tutorial Compiling OCaml Projects:
ocamlbuild
GNU make
OMake
Oasis
(ocamlbuild is probably the easiest tool to use among the four.)
But you may also consider using Dune (formerly known as JBuilder), which is the build system used by a majority of packages in opam.

Related

how to chose between two implementation code in OCaml

I have uploaded the very small exemple project on a github repository
I have a school project. The specifications say that we can enable or disable a component.
I came from the c world in which we can compile c to object and chose the object to link. I try this in OCaml.
I have two source file with the same function but their effect is different.
I have two folders called "on" with implementation and "off" folder with an empty body
For the test, I have a simple a.ml and a.mli file that only print hello world and b.mli b.ml that call the module a. (so I have on/a.ml and off/a.ml)
I compile the on version and the off with this command:
ocamlc -c -I on on/a.mli on/a.ml -o on/a.cmo
then I try to link the C-way
ocamlc on/a.cmo b.ml -o on_b.exe
but I get the error
File "b.ml", line 1, characters 9-15:
Error: Unbound module A
I have then read I should specify the folder to search in with -I.
ocamlc -I on -I off on/a.cmo b.ml -o on_b.exe
I was happy because of that work for the on version
but it will not work for the off version
ocamlc -I on -I off off/a.cmo b.ml -o off_b.exe
I get the error
Error: Files b.cmo and off/a.cmo
make inconsistent assumptions over interface A
I have inspected with ocamlobjinfo it seems to build B its searches for the first module called A
In this example, I have only A and B but in future, I will build with some version on and some off... but don't do it manually
A solution found but no really efficient is to clean all .cmo and .cmi files ...
thanks for your reading and your time
EDIT:
I have also test with -open but it seems to work only for standards modules.
As you can see, compiling your .mli files produces a .cmi file. This is the file that you indicate is there by using the -I dir option.
So, if I understand you correctly, you have a a.mli in both your on and your off directory.
Now, when you refer to the module A in your code, the compiler looks for it in the current directory, then in the linked directories in the order they are given. When you compile "on", the right .cmi is found (because -I on comes before -I off in your command line). In the case of "off", the "on" cmi is found, and the compiler sees that it doesn't correspond to the right cmo.
Your two compiling lines should be:
ocamlc -I on on/a.cmo b.ml -o on_b.exe
ocamlc -I off off/a.cmo b.ml -o off_b.exe

ocamlbuild links libraries in wrong order

I'm somehow trying to use _CoqProject parser from coq's library in OCaml (I'd welcome better alternatives to grab the .v files of a coq project if that library is not meant for external use, is it?), but ocamlbuild seems to be linking libraries in the wrong order.
Consider this minimal example file
open CoqProject_file
let x = read_project_file
The coq.lib package (bundled with coq) somehow depends on threads, and following this answer suggests to use -tag thread for that, but I still get the following error that threads is not found when linking coq.lib:
$ ocamlbuild -pkg coq.lib -tag thread -cflag -rectypes a.native /tmp/p
+ /home/sam/.opam/4.06.0+coq-8.7/bin/ocamlopt.opt -I /home/sam/.opam/4.06.0+coq-8.7/lib/coq/config -I /home/sam/.opam/4.06.0+coq-8.7/lib/coq/lib -I /home/sam/.opam/4.06.0+coq-8.7/lib/ocaml /home/sam/.opam/4.06.0+coq-8.7/lib/ocaml/str.cmxa /home/sam/.opam/4.06.0+coq-8.7/lib/ocaml/unix.cmxa /home/sam/.opam/4.06.0+coq-8.7/lib/coq/lib/clib.cmxa -thread threads.cmxa a.cmx -o a.native
File "_none_", line 1:
Error: No implementations provided for the following modules:
Thread referenced from /home/sam/.opam/4.06.0+coq-8.7/lib/coq/lib/clib.cmxa(Exninfo)
Mutex referenced from /home/sam/.opam/4.06.0+coq-8.7/lib/coq/lib/clib.cmxa(Exninfo)
Command exited with code 2.
Yet that compiles if I take the ocamlopt invocation apart and put -thread threads.cmxa before clib.cmxa
$ cd _build/
$ /home/sam/.opam/4.06.0+coq-8.7/bin/ocamlopt.opt -I /home/sam/.opam/4.06.0+coq-8.7/lib/coq/config -I /home/sam/.opam/4.06.0+coq-8.7/lib/coq/lib -I /home/sam/.opam/4.06.0+coq-8.7/lib/ocaml /home/sam/.opam/4.06.0+coq-8.7/lib/ocaml/str.cmxa /home/sam/.opam/4.06.0+coq-8.7/lib/ocaml/unix.cmxa -thread threads.cmxa /home/sam/.opam/4.06.0+coq-8.7/lib/coq/lib/clib.cmxa a.cmx -o a.native
What is the right way to call ocamlbuild?
If you use ocamlfind packages, you should use the -use-ocamlfind flag.
There is no good solution as to why -tag thread is needed¹. There are two different implementations of the OCaml Threads interface (one with os threads and one with green threads), and coq.lib depends on the interface but won't decide for the user which one to use, so you have to specify it manually, for example by using -tag thread.
¹: one solution would be to remove this choice by deprecating vmthreads (the green threads), which is rarely used in practice.

C++ source compilation using MATLAB Engine and g++

It would be helpful if you could provide some guidance on how to compile c++ source code files in an Ubuntu environment, using the MATLAB Engine with g++.
I assume that you want to know the procedure for compiling the c++ code (which calls MATLAB engine) using g++ from Linux Terminal. To do so, follow the steps below:
Include following paths in PATH variable:
a) Location of MATLAB i.e. $matlabroot/bin
b) $matlabroot/sys/os
You can do this by using the command
'setenv PATH $matlabroot/bin:$matlabroot/sys/os:$PATH ' .
In the command prompt, navigate to the directory where the cpp code is located using cd command. For instance, if you are compiling engdemo.cpp, you need to navigate to $matlabroot/extern/examples/eng_mat/engdemo.cpp
You need to call the compiler with required include files and libraries. For this you can use -I and -L switches for that. Note that the order is important. So you need to use the command as below:
g++ engdemo.cpp -I "$matlabroot/extern/include" -L "$matlabroot/bin/glnxa64" -leng -lmat -lmex -lut -o engdemo.o
The above command will generate an object file engdemo.o. To execute this, use the command ./engdemo.o
You can refer to the document at http://www.umiacs.umd.edu/~jsp/Downloads/MatlabEngine/MatlabEngine.pdf for more help regarding C++ and MATLAB.
The compilation process in C/C++ is divided in two phases:
First, the compilation where source code is transformed into machines code with multiples object files (.o or .obj).
Then, the link to transform object files into a single executable file (.dll or .exe).
C/C++ programs that run matlab engine need three things:
1> A compiler that is compatible with matlab engine.
2> Reference to API header files('.h' for c or '.hpp' for c++) for compilation.
3> Reference to the libraries('.lib' for windows,'.so' for linux) for external symbol link.
You can see comptatible linux based system compiler here.
The GCC C/C++ 4.9.x is compatible so you can use g++.
As this pdf suggested, the API header files should be there $matlabroot/extern/include and the .so files should be in $matlabroot/
bin/glnax64 where $matlabroot is your matlab install folder
Set up Environment variables
Open your temnial with ctrl + alt + T and type :
setenv PATH $matlabroot/bin:$matlabroot/sys/os:$PATH
You can then go to the folder where source file is located, let's say $matlabroot/extern/examples/eng_mat/ with the following command :
cd $matlabroot/extern/examples/eng_mat/
You need to do the compilation with :
g++ -c engDemo.cpp -I '$matlabroot/extern/include' -leng -lmat -lmex -lut
After that, a file named engDemo.o should be created.
The -leng -lmat -lmex -lut options are probably needed among other things because of the usage of the matlab interpreter that should be located in $matlabroot/bin
And the external symbol link with :
g++ -o engDemo -L '$matlabroot/bin/glnax64'
Be careful as this path sugested that you are on a x64 architecture machine, if you are not,the path might be slightly different.
Then you can execute your file just by doing ./engDemo
I can't install the matlab engine on the laptot I am using so I'm unable to test the instruction I gave you but It should be done this way.
Hope it helps !!

How to build coreutils with LLVM 3.4

I am trying to build GNU Coreutils 8.23 using the LLVM 3.4 tool-chain. One very important aspect is that I also need the LLVM bytecode for all the coreutils. Therefore, I need to include -emit-llvm in the CFLAGS. Therefore, I removed the $(CFLAGS) from the LINK variable of the coreutils Makefile. Afterwards, I run the following command:
make CC=/home/user/llvm-3.4.2/build/Release+Asserts/bin/clang
CCLD=/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-link
IGNORE_UNUSED_LIBRARIES_CFLAGS= CFLAGS="-emit-llvm -S"
VERBOSE=1 AM_CFLAGS= AM_LDFLAGS=
AR=/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-ar
RANLIB=/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-ranlib
and I get the following error:
/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-link: src/libver.a:1:2: error: expected integer
!<arch>
^
/home/user/llvm-3.4.2/build/Release+Asserts/bin/llvm-link: error loading file 'src/libver.a'
Any ideas of how to get this to work?
Try this.
export CC="/home/user/llvm-3.4.2/build/Release+Asserts/bin/clang"
export CXX="/home/user/llvm-3.4.2/build/Release+Asserts/bin/clang++"
Make sure this is where your compiler toolchain is present.
Then in the the coreutils directory, run ./configure (before this run ./bootstrap if you havent already run it). Running ./configure checks if your clang can compile properly and creates a Makefile with the correct configuration.
Then do a make and make install as instructed.
Lib file '.a' here is not readable by llvm-link.
A possible informal hack to this probably is to find out the Makefile generating this lib, and let
AR = llvm-link, ar option = -o(i.e. change ar rv to llvm-link -o),
and disable ranlib command while compling(you don't need ranlib if using llvm-link).
Then the '.a' file generated is a stitched bc file, and this '.a' file should be accpetable by llvm-link command you are calling

Simple libtool alternative?

Being perfectly satisfied with old-style Makefiles, I am looking for a simple alternative to libtool. I do not want to switch to automake, and I keep running into problems with libtool when I try to use it directly. The latest one is 'unsupported hardcode properties', and I am getting fed up with the lack of complete documentation that just tells me what is wrong this time...
I only want to compile a bunch of .o files with the right flags and then link them into a shared library, such that it works on as many platforms as possible. Is there anything out there that does just that and not force me to switch all of my other tools at the same time?
I not sure if it would fit info your workflow but I'd recommend looking at CMake. It works on Windows, Linux and Mac and should not force you to change any of your other tools. You'll have to judge its suitability yourself though.
There's jlibtool (which has nothing to do with java).
It's written in C, and can just be bundled with your source.
It was originally an apache project, but whoever was working it there seems to of abandoned it around 2004.
It was taken over by FreeRADIUS project maintainer Alan Dekok, who modernised the code and fixed a few niggling issues. We use it for the FreeRADIUS project (>= 3.0.0) to do all the build time linking.
Given your description in the comment to Milliams' answer,
I just want one tool that I tell: "give me the compiler flags so that I can compile these n files for use in a shared library, and then give me the commands to link them together",
then libtool may well be the simplest tool for the job. I know of no other alternative.
You are right that the documentation for using libtool with plain makefiles is practically nonexistent, but libtool certainly does not require you to switch to automake. Cross-platform libraries are difficult, and the price you have to pay for them is libtool. (Or maybe the discount price is libtool+automake+autoconf or CMake or Jam.)
slibtool (dl.midipix.org/slibtool, git://midipix.org/slibtool) is a libtool drop-in replacement, written in C. A single slibtool binary aims to seamlessly support both native and cross-builds, and the utility also provides some additional features (installation of .la files is optional, optional color-coded annotation, etc). The following minimal plain makefile demonstrates how to (cross-) build a library using slibtool.
CC = cc
LIBTOOL = slibtool
DESTDIR = destdir
all: libfoo.la
a.lo:
$(LIBTOOL) --mode=compile --tag=CC $(CC) -c a.c
libfoo.la: a.lo
$(LIBTOOL) --mode=link --tag=CC $(CC) -o libfoo.la -rpath /lib
install: all
mkdir -p destdir
$(LIBTOOL) --mode=install cp libfoo.la $(DESTDIR)
# the -rpath argument is required for semantic compatibility with libtool.
native build, default (both shared library and static library)
$ make
$ make install
native build, shared library only
$ make LIBTOOL=slibtool-shared
$ make install
native build, static library only
$ make LIBTOOL=slibtool-static
$ make install
cross-build, default
$ make CC=some-target-tuple-gcc
$ make install
cross-build, default, with lots of colors
$ make LIBTOOL=dlibtool CC=some-target-tuple-gcc
$ make install