Search code examples
moduleocamlbuild-systemopam

In OCaml, two third party libraries expose same module name. Failure to link


My project is using ctypes library and markup library. When compile, it gives following complain:

Error: Files /Users/Young/.opam/4.02.1/lib/markup/markup.cmxa
       and /Users/Young/.opam/4.02.1/lib/ctypes/ctypes.cmxa
       both define a module named Common

Obviously, both libraries expose same module name. How to fix it? Any help will be appreciated. Thanks.

Update: Also I don't quite understand why there'll be such a conflict. To my understanding, even if both libraries expose the same module Common, they will appear as Ctypes.Common vs Markup.Common. There's should be no conflict?


Solution

  • In general, you can't solve this without modifying an upstream, so in case of such errors, you need to ask the library maintainers to rename corresponding modules.

    In your case, using a newer version of ctypes library should help, as on mine machine the ctypes (version 0.11.2) library no longer defines the Common module.

    Update: Also I don't quite understand why there'll be such a conflict. To my understanding, even if both libraries expose the same module Common, they will appear as Ctypes.Common vs Markup.Common. There's should be no conflict?

    Your understanding is more or less correct. There are few details however, a library is a collection of compilation units, each compilation unit is a collection of modules. Compilation units basically maps to files, e.g., if you have file common.ml, then the compilation unit would have the common name. Libraries and compilation units live in a flat namespace, modules in general leave in a hierarchical namespace. That means, that filenames from which libraries are built must have different names, that's why if you will look at the latest codebase, you will notice that all filenames are prefixed with ctypes_. There is a partial solution, to mitigate this namespace problem one can use packed modules, in that case only one module (usually with the same name as the library).