This is a walkthrough on how to setup a project so we can run some
http requests against the
Twitter API with
OCaml. If you already now how to do this, you can skip to the next articles in this series. Have at it!
opamif you don't have it. Check the install instructions on the link on the opam home page.
After you have it installed make a director for this project and
mkdir ocaml-twitter-postman && cd ocaml-twitter-postman.
We need to create a compiler specific to this project which we will do in our root directory which is the
/ocaml-twitter-postmandir, by running
opam switch create . ocaml-base-compiler.4.08.1. This part takes a while so go for a walk around the block. This
switchis the one we will use to compile our
OCamlcode in all the examples. After the process finishes lets do what the output is telling us and run
eval $(opam env)to update the current shell environment so it knows to use this compiler.
We are going to need
duneto run some commands so let's install it with:
opam install dune.
Now let's make a directory for first example,
mkdir hello-postman && cd !$then create a
dunefile and initiate
dune installcommand we run below creates a
dune-projectfile we wont ever look at again. Run:
touch dune && dune install # running `tree` in your terminal gets you: . ├── _build │ └── log ├── dune └── dune-project 1 directory, 3 files
- Create a
hello_postman.mlfile in the root with the following contents:
Lwt_main.run (Lwt_io.printf "Hello, postman!\n")
- Configure the
dunefile by adding the following to it:
(executable (name hello_postman) (libraries lwt.unix))
- Lets add a
Makefileto simplify some running commands. Run
touch Makefileand the following to it:
INSTALL_ARGS := $(if $(PREFIX),--prefix $(PREFIX),) default: dune build install: dune install $(INSTALL_ARGS) uninstall: dune uninstall $(INSTALL_ARGS) reinstall: uninstall install clean: dune clean .PHONY: default install uninstall reinstall clean
I don't remember where I stole this file from but thank you, nonetheless.
- To build and run the project run the following commands from the
makewill run the script at the top of the make file then the
defaultargument in which is the
buildcommand. This will install any dependencies we listed in our
dunefile then build the project. This is doing the same thing as when we ran
dune installabove. So if you run
make cleanit will get rid of all the generated files. You can then just run
makeand your deps will be installed and
dune-projectwill be created. #NoteToSelf: Its worth remembering that we are using the compiler we created in the root repository.
make clean make dune exec ./hello_postman.exe # output ➜ hello-postman make dune build Info: Creating file dune-project with this contents: | (lang dune 2.1) ➜ hello-postman dune exec ./hello_postman.exe Hello, postman!
- Add a
OCaml.gitignorefile. You can find the
The source code for this post is here.
That was quick. Check out Part 2 to get a bit more serious.