Jackson Tale
Jackson Tale

Reputation: 25842

How can I write a library in OCaml?

I am writing a bson encoder/decoder library in ocaml.

I have the source file now (actually just one file).

My question is that how should I make it as a library, such as ocaml-batteries-included, camomile, etc?

I mean, people can use opam install it and use ocamlfind + ocamlbuild to compile with it?

Upvotes: 1

Views: 551

Answers (3)

lambdapower
lambdapower

Reputation: 1032

Go for Oasis; you basically create a file _oasis that describes your library. The oasis tool will then create a build system for you. You can install Oasis via Opam. There is a quick start tutorial for Oasis that will get you started.

Oasis is an effort to standardize a computer readable description of OCaml packages. And while at it, it can create the build system as a by-product (currently an ocamlbuild based one and Makefiles are supported).

Upvotes: 1

rgrinberg
rgrinberg

Reputation: 9878

I will pitch something that is a little non standard but entirely usable: obuild. All you need is a project.obuild file and then obuild build will build your project. obuild install will install it with ocamlfind.

Install obuild from github instead of opam however since the opam version is a little out of date. Here's a sample obuild file to give you an idea: https://github.com/vbmithr/ocaml-websocket/blob/master/websocket.obuild

Upvotes: 2

gasche
gasche

Reputation: 31479

Supporting ocamlfind is really easy, and there is no excuse not to do it. Basically, pick any "similar" library, look for the META file, and adapt it to your own setting. I have given some basic instructions in this StackOverflow answer.

OCamlbuild is a build system that you are free to decide to use or not, but should not impact your users (besides the fact that they will to install it before building your programs). You may also use a Makefile or any other build system (omake, etc.). The idea of ocamlbuild is just to provide a _tags file with sufficient information to build your library/program (and if you're doing advanced stuff a myocamlbuild.ml file as well). Again, look at what projects that have a structure similar to yours are doing.

For Opam, there is a packaging guide to give your directions (but again, just adapt what other people are doing).

Upvotes: 4

Related Questions