Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] What's the purpose of the static library?

62 views
Skip to first unread message

bill yan

unread,
Sep 21, 2008, 11:42:03 PM9/21/08
to caml...@inria.fr
Hi,

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.

Regards,
Bill

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Stéphane Glondu

unread,
Sep 22, 2008, 5:35:45 AM9/22/08
to bill yan, caml...@inria.fr
bill yan a écrit :

> 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.


Cheers,

--
Stéphane

bill yan

unread,
Sep 23, 2008, 5:16:00 AM9/23/08
to Stéphane Glondu, caml...@inria.fr
Hi Stéphane,

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.

Regards,
Bill

Stéphane Glondu 已写入:

> bill yan a écrit :
>
>> 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.
>
>
> Cheers,
>

_______________________________________________

Richard Jones

unread,
Sep 23, 2008, 6:17:24 AM9/23/08
to bill yan, Stéphane Glondu, caml...@inria.fr
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.

Created by: 'ocamlmklib -o library *.o *.cmo'
or: 'ocamlmklib -o library *.o *.cmx'

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.

----------------------------------------------------------------------

Rich.

--
Richard Jones
Red Hat

Daniel Bünzli

unread,
Sep 23, 2008, 6:25:32 AM9/23/08
to OCaml Mailing List

Le 23 sept. 08 à 11:09, bill yan a écrit :

> 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.

o Part III of the manual (especially ocamlc ocamlopt and interfacing
with C). http://caml.inria.fr/pub/docs/manual-ocaml/
o Chapter 7 and 12 of the Oreilly book http://caml.inria.fr/pub/docs/oreilly-book/html/index.html

Best,

Daniel

Alain Frisch

unread,
Sep 23, 2008, 7:42:59 AM9/23/08
to Richard Jones, Stéphane Glondu, bill yan, caml...@inria.fr
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.
>
> Created by: 'ocamlmklib -o library *.o *.cmo'
> or: 'ocamlmklib -o library *.o *.cmx'
>
> Used: dlllibrary.so is dlopen(2)'d by the toplevel.
> liblibrary.a is linked in standalone programs.

dlllibrary.so is also used by the bytecode interpreter, by Dynlink and
by ocamlc (to check for the availability of C primitives at compile time).


-- Alain

Chris Conway

unread,
Sep 23, 2008, 8:50:28 AM9/23/08
to caml...@inria.fr

Richard Jones <rich <at> annexia.org> writes:
> <...>

> 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

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.

Regards,
Chris

Stefano Zacchiroli

unread,
Sep 24, 2008, 7:26:04 AM9/24/08
to caml...@inria.fr, Debian Ocaml Maint ML
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

Richard Jones

unread,
Sep 24, 2008, 1:29:06 PM9/24/08
to caml...@inria.fr, Debian Ocaml Maint ML
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.

http://ocaml-tutorial.org/filenames

is probably the best place. I've added a link to my posting
temporarily.

Rich.

--
Richard Jones
Red Hat

_______________________________________________

Sylvain Le Gall

unread,
Sep 24, 2008, 4:10:15 PM9/24/08
to caml...@inria.fr
On 23-09-2008, Richard Jones <ri...@annexia.org> wrote:
> 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.
>

Do you think *.cmx and *.o should be shipped?

Is *.cmx enough?

Regards
Sylvain Le Gall

bill yan

unread,
Sep 25, 2008, 7:04:51 AM9/25/08
to Richard Jones, Stéphane Glondu, caml...@inria.fr
Hi Richard,

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?

Thanks & Best regards,
Bill

Richard Jones ???:

Alain Frisch

unread,
Sep 25, 2008, 7:11:32 AM9/25/08
to bill yan, Stéphane Glondu, caml...@inria.fr, Richard Jones
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).


Alain

bill yan

unread,
Oct 7, 2008, 11:27:00 PM10/7/08
to Alain Frisch, Stéphane Glondu, caml...@inria.fr, Richard Jones
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

unread,
Oct 7, 2008, 11:35:29 PM10/7/08
to Alain Frisch, Stéphane Glondu, caml...@inria.fr, Richard Jones
Sorry, I just noticed Richard already answered my question in previous
email. So Please ignore my previous email. Thanks.

bill yan 已写入:

0 new messages