Hi !
I’m trying to compile a very simple C++ project for OCaml. Here is what I did so far:
#include <iostream>
#include <string>
#include "osm.hpp"
extern "C" {
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>
}
extern "C"
CAMLprim value osm_from_file(value file)
{
CAMLparam1 (file);
std::string fileName = String_val(file);
std::cout << fileName << std::endl;
CAMLreturn (Val_unit);
}
extern "C"
CAMLprim value osm_read_data(value osmObj)
{
CAMLparam1 (osmObj);
std::cout << "Testing" << std::endl;
CAMLreturn (Val_unit);
}
I’m compiling it using CMake for the “osm” little class I’m writing and Dune to link it with my OCaml project.
Here is what I get while building my project with Dune.
$>> dune build
ocamlopt project/bin/main.exe (exit 2)
(cd _build/default && /home/user/.opam/4.12.0/bin/ocamlopt.opt -w @1..3@5..28@30..39@43@46..47@49..57@61..62-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -o project/bin/main.exe project/lib/project.cmxa project/bin/osm/cosm.cmxa -I project/bin/osm project/bin/.main.eobjs/native/dune__exe.cmx project/bin/.main.eobjs/native/dune__exe__Map.cmx project/bin/.main.eobjs/native/dune__exe__Main.cmx -linkall -cclib -lstdc++)
/usr/bin/ld: project/bin/osm/libbinding.a(binding.cpp.o): in function `osm_from_file':
binding.cpp:(.text+0x55e): undefined reference to `caml_local_roots'
/usr/bin/ld: binding.cpp:(.text+0x56c): undefined reference to `caml_local_roots'
/usr/bin/ld: binding.cpp:(.text+0x57b): undefined reference to `caml_local_roots'
/usr/bin/ld: binding.cpp:(.text+0x61f): undefined reference to `caml_local_roots'
/usr/bin/ld: project/bin/osm/libbinding.a(binding.cpp.o): in function `osm_read_data':
binding.cpp:(.text+0x6ba): undefined reference to `caml_local_roots'
/usr/bin/ld: project/bin/osm/libbinding.a(binding.cpp.o):binding.cpp:(.text+0x6c5): more undefined references to `caml_local_roots' follow
collect2: error: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking (exit code 1)
caml_local_roots should be defined in “caml/memory.h” but it looks like there is a problem while linking this file with my project.
Here is my Dune file to compile the “osm” library.
(library
(name cosm)
(c_library_flags :standard -lstdc++ -lbz2 -lexpat -lz -lpthread)
(foreign_archives binding)
)
(rule
(deps (source_tree osm))
(targets libbinding.a dllbinding.so)
(action
(no-infer
(progn
(chdir osm (run make))
(copy osm/libbindingsh.so dllbinding.so)
(copy osm/libbinding.a libbinding.a)
))))
Does anyone has an idea on how can I solve this problem ?