I noticed there are some static libraries(.a) installed with ocaml, for example, /usr/lib/ocaml/bigarray.a. What's the purpose of those static libraries? Thanks a lot.
> I noticed there are some static libraries(.a) installed with ocaml, for > example, /usr/lib/ocaml/bigarray.a. What's the purpose of those static > libraries? Thanks a lot.
They contain (natively) compiled OCaml code. An OCaml library compiled in native mode (usually) consists of a .a and a .cmxa file, the former containing OCaml-specific information.
They are not to be confused with lib*.a files, which contain compiled C stubs, and are needed for generating native-code executables, and bytecode ones in -custom mode.
Thanks a lot for your information. And we'd like to know more about the OCaml library architectures, like on what situation dynamic libraries are used, and when static libraries are used, and so on.. Really appreciate if you could point me to a document that can help on this topic.
>> I noticed there are some static libraries(.a) installed with ocaml, for >> example, /usr/lib/ocaml/bigarray.a. What's the purpose of those static >> libraries? Thanks a lot.
> They contain (natively) compiled OCaml code. An OCaml library compiled > in native mode (usually) consists of a .a and a .cmxa file, the former > containing OCaml-specific information.
> They are not to be confused with lib*.a files, which contain compiled > C stubs, and are needed for generating native-code executables, and > bytecode ones in -custom mode.
On Tue, Sep 23, 2008 at 05:09:58PM +0800, bill yan wrote: > Thanks a lot for your information. And we'd like to know more about the > OCaml library architectures, like on what situation dynamic libraries > are used, and when static libraries are used, and so on.. Really > appreciate if you could point me to a document that can help on this topic.
It's not particularly well-documented, and it changes a little in 3.11, but below is my understanding. There are probably errors in what follows. If someone can correct the errors then I'll publish a corrected document.
File: module.cmi ------------------------------
Contains the compiled interface for Module, essentially equivalent to the contents of the *.mli file but in a compiled/binary form which the compiler can load easily.
Created by: 'ocamlc -c module.mli', or if module.mli doesn't exist then 'ocamlc -c module.ml' (also by 'ocamlopt -c module.ml').
Used: Whenever the toplevel or compiler uses any symbol Module.foo, this file is consulted.
File: module.cmo ------------------------------
Contains the bytecode of the implementation of Module.
Created by: 'ocamlc -c module.ml'
Used: When linking bytecode programs, or creating bytecode libraries (*.cma), or by the toplevel when you use #load, or by Dynlink.
File: library.cma ------------------------------
This is just a set of *.cmo files combined together.
Created by: 'ocamlc -a'
Used: Same as for module.cmo
Files: module.o and module.cmx --------------------
These two files go together. The *.o file contains compiled native code in the normal system object file format. The *.cmx file contains metainformation about the machine code in the *.o file.
Created by: 'ocamlopt -c module.ml'
Used: When linking native code programs, or creating native code libraries.
Note(1): You normally never need to specify the *.o files by hand. On the command line when the compiler sees a *.cmx file, it looks for the corresponding *.o file if it needs it.
Note(2): It is thought that the *.cmx file needs to be around even when linking a library (*.cmxa) file in order to do cross-module function inlining. Both the Debian & Fedora packaging rules specify that *.cmx files be kept around for this reason. Whether this is really true or not is not certain.
File: library.a and library.cmxa --------------------
These files go together. The *.a file contains compiled native code in the normal system archive format. The *.cmxa file contains metainformation.
Created by: 'ocamlopt -a'
Used: Same as for *.o/*.cmx
Note: You normally never need to specify the *.a files by hand.
File: dlllibrary.so and liblibrary.a --------------------
These files are created and used when a bytecode or native library contains some C code. 'dllXXX.so' is created for use by the toplevel and contains the compiled C code. 'libXXX.a' is created for use by compiled standalone programs and also contains the same compiled C code.
Used: dlllibrary.so is dlopen(2)'d by the toplevel. liblibrary.a is linked in standalone programs.
Note: You normally never need to specify these files by hand. The *.cma/*.cmxa file contains the necessary information to find these files if necessary.
> And we'd like to know more about the OCaml library architectures, > like on what situation dynamic libraries are used, and when static > libraries are used, and so on..
This depends on what _you_ want. The tradeoffs of dynamic vs static linking are mostly as in any other language.
> Really appreciate if you could point me to a document that can help > on this topic.
Richard Jones wrote: > File: library.cma ------------------------------
> This is just a set of *.cmo files combined together.
> Created by: 'ocamlc -a'
> Used: Same as for module.cmo
cma files also contain extra linking directives like references to C libraries.
> Files: module.o and module.cmx --------------------
> These two files go together. The *.o file contains compiled native > code in the normal system object file format. The *.cmx file contains > metainformation about the machine code in the *.o file.
> Created by: 'ocamlopt -c module.ml'
> Used: When linking native code programs, or creating native code > libraries.
.. or compiling other modules.
> Note(1): You normally never need to specify the *.o files by hand. On > the command line when the compiler sees a *.cmx file, it looks for the > corresponding *.o file if it needs it.
> Note(2): It is thought that the *.cmx file needs to be around even > when linking a library (*.cmxa) file in order to do cross-module > function inlining. Both the Debian & Fedora packaging rules specify > that *.cmx files be kept around for this reason. Whether this is > really true or not is not certain.
This is true.
cmx files are needed when they contain modules compiled with -for-pack. Otherwise, they are optional. Hiding them to the compiler is a way to get fewer dependencies (more separate compilation).
Note that the .o extension is actually .obj for the MSVC ports under Windows.
> File: library.a and library.cmxa --------------------
> These files go together. The *.a file contains compiled native code > in the normal system archive format. The *.cmxa file contains > metainformation.
> Created by: 'ocamlopt -a'
> Used: Same as for *.o/*.cmx
> Note: You normally never need to specify the *.a files by hand.
(.lib for MSVC ports)
> File: dlllibrary.so and liblibrary.a --------------------
> These files are created and used when a bytecode or native library > contains some C code. 'dllXXX.so' is created for use by the toplevel > and contains the compiled C code. 'libXXX.a' is created for use by > compiled standalone programs and also contains the same compiled C > code.
> This is just a set of *.cmo files combined together.
> Created by: 'ocamlc -a'
> Used: Same as for module.cmo
Richard,
This is a wonderful answer! It should go onto the wiki or in the FAQ. One nitpick...
cma (and .cmxa) files should not be used for creating libraries (as .cmo and .cmx files can (and should) be). I.e., don't like a library into a library. Doing so will give a compiler error: X.cma is not a compilation unit description.
On Tue, Sep 23, 2008 at 11:17:11AM +0100, Richard Jones wrote: > It's not particularly well-documented, and it changes a little in > 3.11, but below is my understanding. There are probably errors in > what follows. If someone can correct the errors then I'll publish a > corrected document.
Hi Richard, do you have a public place where this document (patched with the received comments) is available? If so please let us know (so that we can reference if from the Debian OCaml packaging policy), if not I will integrate it directly in the policy document, which is in fact publicly available on the web.
Cheers.
-- Stefano Zacchiroli -*- PhD in Computer Science \ PostDoc @ Univ. Paris 7 zack@{upsilon.cc,pps.jussieu.fr,debian.org} -<>- http://upsilon.cc/zack/ I'm still an SGML person,this newfangled /\ All one has to do is hit the XML stuff is so ... simplistic -- Manoj \/ right keys at the right time
On Wed, Sep 24, 2008 at 01:24:54PM +0200, Stefano Zacchiroli wrote: > On Tue, Sep 23, 2008 at 11:17:11AM +0100, Richard Jones wrote: > > It's not particularly well-documented, and it changes a little in > > 3.11, but below is my understanding. There are probably errors in > > what follows. If someone can correct the errors then I'll publish a > > corrected document.
> Hi Richard, > do you have a public place where this document (patched with the > received comments) is available? If so please let us know (so that we > can reference if from the Debian OCaml packaging policy), if not I will > integrate it directly in the policy document, which is in fact publicly > available on the web.
> On Tue, Sep 23, 2008 at 05:09:58PM +0800, bill yan wrote:
> Files: module.o and module.cmx --------------------
> These two files go together. The *.o file contains compiled native > code in the normal system object file format. The *.cmx file contains > metainformation about the machine code in the *.o file.
> Created by: 'ocamlopt -c module.ml'
> Used: When linking native code programs, or creating native code > libraries.
> Note(1): You normally never need to specify the *.o files by hand. On > the command line when the compiler sees a *.cmx file, it looks for the > corresponding *.o file if it needs it.
> Note(2): It is thought that the *.cmx file needs to be around even > when linking a library (*.cmxa) file in order to do cross-module > function inlining. Both the Debian & Fedora packaging rules specify > that *.cmx files be kept around for this reason. Whether this is > really true or not is not certain.
Thanks a lot for your help on this topic. Now I have a quesiton for the following part:
File: library.a and library.cmxa --------------------
These files go together. The *.a file contains compiled native code in the normal system archive format. The *.cmxa file contains metainformation.
By my understanding, unlike dlllibrary.so and liblibrary.a give user an option to choose compile dynamically or staticly, it seems for library.a, user can only choose static method. Does that mean "compiled native code" can only be staticly linked to user's application?
>On Tue, Sep 23, 2008 at 05:09:58PM +0800, bill yan wrote:
>>Thanks a lot for your information. And we'd like to know more about the >>OCaml library architectures, like on what situation dynamic libraries >>are used, and when static libraries are used, and so on.. Really >>appreciate if you could point me to a document that can help on this topic.
>It's not particularly well-documented, and it changes a little in >3.11, but below is my understanding. There are probably errors in >what follows. If someone can correct the errors then I'll publish a >corrected document.
>File: module.cmi ------------------------------
>Contains the compiled interface for Module, essentially equivalent to >the contents of the *.mli file but in a compiled/binary form which the >compiler can load easily.
>Created by: 'ocamlc -c module.mli', or if module.mli doesn't exist >then 'ocamlc -c module.ml' (also by 'ocamlopt -c module.ml').
>Used: Whenever the toplevel or compiler uses any symbol Module.foo, >this file is consulted.
>File: module.cmo ------------------------------
>Contains the bytecode of the implementation of Module.
>Created by: 'ocamlc -c module.ml'
>Used: When linking bytecode programs, or creating bytecode libraries >(*.cma), or by the toplevel when you use #load, or by Dynlink.
>File: library.cma ------------------------------
>This is just a set of *.cmo files combined together.
>Created by: 'ocamlc -a'
>Used: Same as for module.cmo
>Files: module.o and module.cmx --------------------
>These two files go together. The *.o file contains compiled native >code in the normal system object file format. The *.cmx file contains >metainformation about the machine code in the *.o file.
>Created by: 'ocamlopt -c module.ml'
>Used: When linking native code programs, or creating native code >libraries.
>Note(1): You normally never need to specify the *.o files by hand. On >the command line when the compiler sees a *.cmx file, it looks for the >corresponding *.o file if it needs it.
>Note(2): It is thought that the *.cmx file needs to be around even >when linking a library (*.cmxa) file in order to do cross-module >function inlining. Both the Debian & Fedora packaging rules specify >that *.cmx files be kept around for this reason. Whether this is >really true or not is not certain.
>File: library.a and library.cmxa --------------------
>These files go together. The *.a file contains compiled native code >in the normal system archive format. The *.cmxa file contains >metainformation.
>Created by: 'ocamlopt -a'
>Used: Same as for *.o/*.cmx
>Note: You normally never need to specify the *.a files by hand.
>File: dlllibrary.so and liblibrary.a --------------------
>These files are created and used when a bytecode or native library >contains some C code. 'dllXXX.so' is created for use by the toplevel >and contains the compiled C code. 'libXXX.a' is created for use by >compiled standalone programs and also contains the same compiled C >code.
>Used: dlllibrary.so is dlopen(2)'d by the toplevel. > liblibrary.a is linked in standalone programs.
>Note: You normally never need to specify these files by hand. The >*.cma/*.cmxa file contains the necessary information to find these >files if necessary.
bill yan wrote: > By my understanding, unlike dlllibrary.so and liblibrary.a give user an > option to choose compile dynamically or staticly, it seems for > library.a, user can only choose static method. Does that mean "compiled > native code" can only be staticly linked to user's application?
In OCaml 3.11, it will be possible to link native code (found in library.a or module.o files) into .cmxs files that can be explicitly loaded at runtime (with the same API as for bytecode Dynlink).
Thanks for the help. Another question, It seems *.cma *.cmxa *.a are all native code libraries, why are there so many suffix? Is there any difference between cma, cmxa and a?
>> By my understanding, unlike dlllibrary.so and liblibrary.a give user >> an option to choose compile dynamically or staticly, it seems for >> library.a, user can only choose static method. Does that mean >> "compiled native code" can only be staticly linked to user's >> application?
> In OCaml 3.11, it will be possible to link native code (found in > library.a or module.o files) into .cmxs files that can be explicitly > loaded at runtime (with the same API as for bytecode Dynlink).
> Thanks for the help. Another question, It seems *.cma *.cmxa *.a are > all native code libraries, why are there so many suffix? Is there any > difference between cma, cmxa and a?
> Alain Frisch 已写入:
>> bill yan wrote:
>>> By my understanding, unlike dlllibrary.so and liblibrary.a give user >>> an option to choose compile dynamically or staticly, it seems for >>> library.a, user can only choose static method. Does that mean >>> "compiled native code" can only be staticly linked to user's >>> application?
>> In OCaml 3.11, it will be possible to link native code (found in >> library.a or module.o files) into .cmxs files that can be explicitly >> loaded at runtime (with the same API as for bytecode Dynlink).