caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* compiling C library wrapper
@ 2009-11-05 22:32 Aaron Bohannon
  2009-11-05 23:09 ` [Caml-list] " David Allsopp
  0 siblings, 1 reply; 5+ messages in thread
From: Aaron Bohannon @ 2009-11-05 22:32 UTC (permalink / raw)
  To: caml-list

Hi,

I am quite confused by the whole process of compiling and installing
wrappers for C libraries.  It seems like I can get things to work OK
without really knowing what I'm doing if everything is put and built
in a single directory.  The hard part seems to be putting the right
files in the right places and getting the path arguments correct.
Then things stop working, and I have to really understand what's going
on, but the manual doesn't explain this part of the process in any
detail.

Let's say I have a library "/opt/local/lib/libfoo.a" for which I want
to build and use a (native-code) OCaml wrapper.  Here are the steps as
I understand them:

1) Write the file "foo_stubs.c" and compile it to get "foo_stubs.o".

2) Build the library "libfoo_stubs.a" by running

ar rc libfoo_stubs.a foo_stubs.o

3) Copy "libfoo_stubs.a" to its permanent location, let's say,
"/usr/local/lib/ocaml/stubs/libfoo_stubs.a".

4) Write "foo.mli" and use it to build "foo.cmi"

5) Write "foo.ml" and use it to build "foo.cmxa" by running

ocamlopt -a -o foo.cmxa foo.ml
  -ccopt -L/opt/local/lib -cclib -lfoo
  -ccopt -L/usr/local/lib/ocaml/stubs -cclib -lfoo_stubs

6) Copy "foo.cmi" and "foo.cmxa" to their permanent location, let's
say "/usr/local/lib/ocaml/foo/"

7) Write my file "bar.ml" that needs to use the library, and compile
it by running

ocamlopt -I /usr/local/lib/ocaml/foo -o bar foo.cmxa bar.ml

However, this command ends up passing gcc the filename

/usr/local/lib/ocaml/foo/foo.a

which doesn't exist, so I get an error.  Where did this filename come
from?  Where did I go wrong?

 - Aaron


^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [Caml-list] compiling C library wrapper
  2009-11-05 22:32 compiling C library wrapper Aaron Bohannon
@ 2009-11-05 23:09 ` David Allsopp
  2009-11-05 23:38   ` Aaron Bohannon
  0 siblings, 1 reply; 5+ messages in thread
From: David Allsopp @ 2009-11-05 23:09 UTC (permalink / raw)
  To: 'Aaron Bohannon', caml-list

Aaron Bohannon wrote: 
> I am quite confused by the whole process of compiling and installing
> wrappers for C libraries.  It seems like I can get things to work OK
> without really knowing what I'm doing if everything is put and built
> in a single directory.  The hard part seems to be putting the right
> files in the right places and getting the path arguments correct.
> Then things stop working, and I have to really understand what's going
> on, but the manual doesn't explain this part of the process in any
> detail.

Have a look at ocamlmklib in the manual - if it's available on your platform
then it nicely hides away a lot of this.

<snip - steps 1-4 are fine>
> 5) Write "foo.ml" and use it to build "foo.cmxa" by running
> 
> ocamlopt -a -o foo.cmxa foo.ml
>   -ccopt -L/opt/local/lib -cclib -lfoo
>   -ccopt -L/usr/local/lib/ocaml/stubs -cclib -lfoo_stubs

This command will also build foo.cmx, foo.a and foo.o

> 6) Copy "foo.cmi" and "foo.cmxa" to their permanent location, let's
> say "/usr/local/lib/ocaml/foo/"

You should also copy foo.a to this directory which should fix the problem.
foo.cmxa contains information required by OCaml but the actual code is in
foo.a (as it's been natively compiled). Similarly, .cmx files contain
information which ocamlopt needs but the actual code is in .o (or .obj)
files. For native code, it's a code idea to copy the .cmx files too as it
allows ocamlopt to do some inlining (I think that's right...)

> 7) Write my file "bar.ml" that needs to use the library, and compile
> it by running
> 
> ocamlopt -I /usr/local/lib/ocaml/foo -o bar foo.cmxa bar.ml

This should now work without error.


David



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] compiling C library wrapper
  2009-11-05 23:09 ` [Caml-list] " David Allsopp
@ 2009-11-05 23:38   ` Aaron Bohannon
  2009-11-07 12:25     ` Richard Jones
  0 siblings, 1 reply; 5+ messages in thread
From: Aaron Bohannon @ 2009-11-05 23:38 UTC (permalink / raw)
  To: David Allsopp; +Cc: caml-list

Ah!  Yes, that's exactly the part I didn't understand and everything
works fine now.  Thank you!

I can't say I completely understand the byte-code case, but I don't
have an urgent need to.

ocamlmklib seems fine, but if it's just a matter of saving keystrokes,
"make" does a pretty good job of that (as long as I know what to tell
"make" to do).

 - Aaron

On Thu, Nov 5, 2009 at 6:09 PM, David Allsopp <dra-news@metastack.com> wrote:
> Aaron Bohannon wrote:
>> I am quite confused by the whole process of compiling and installing
>> wrappers for C libraries.  It seems like I can get things to work OK
>> without really knowing what I'm doing if everything is put and built
>> in a single directory.  The hard part seems to be putting the right
>> files in the right places and getting the path arguments correct.
>> Then things stop working, and I have to really understand what's going
>> on, but the manual doesn't explain this part of the process in any
>> detail.
>
> Have a look at ocamlmklib in the manual - if it's available on your platform
> then it nicely hides away a lot of this.
>
> <snip - steps 1-4 are fine>
>> 5) Write "foo.ml" and use it to build "foo.cmxa" by running
>>
>> ocamlopt -a -o foo.cmxa foo.ml
>>   -ccopt -L/opt/local/lib -cclib -lfoo
>>   -ccopt -L/usr/local/lib/ocaml/stubs -cclib -lfoo_stubs
>
> This command will also build foo.cmx, foo.a and foo.o
>
>> 6) Copy "foo.cmi" and "foo.cmxa" to their permanent location, let's
>> say "/usr/local/lib/ocaml/foo/"
>
> You should also copy foo.a to this directory which should fix the problem.
> foo.cmxa contains information required by OCaml but the actual code is in
> foo.a (as it's been natively compiled). Similarly, .cmx files contain
> information which ocamlopt needs but the actual code is in .o (or .obj)
> files. For native code, it's a code idea to copy the .cmx files too as it
> allows ocamlopt to do some inlining (I think that's right...)
>
>> 7) Write my file "bar.ml" that needs to use the library, and compile
>> it by running
>>
>> ocamlopt -I /usr/local/lib/ocaml/foo -o bar foo.cmxa bar.ml
>
> This should now work without error.
>
>
> David
>
>
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] compiling C library wrapper
  2009-11-05 23:38   ` Aaron Bohannon
@ 2009-11-07 12:25     ` Richard Jones
  2009-11-07 16:34       ` Aaron Bohannon
  0 siblings, 1 reply; 5+ messages in thread
From: Richard Jones @ 2009-11-07 12:25 UTC (permalink / raw)
  To: Aaron Bohannon; +Cc: David Allsopp, caml-list

On Thu, Nov 05, 2009 at 06:38:05PM -0500, Aaron Bohannon wrote:
> Ah!  Yes, that's exactly the part I didn't understand and everything
> works fine now.  Thank you!
> 
> I can't say I completely understand the byte-code case, but I don't
> have an urgent need to.
> 
> ocamlmklib seems fine, but if it's just a matter of saving keystrokes,
> "make" does a pretty good job of that (as long as I know what to tell
> "make" to do).

For the install step, use 'ocamlfind install'.

Grab the latest source tarball for one of our projects,
eg. libvirt-ocaml, to see how we do it:

http://libvirt.org/sources/ocaml/
or: http://libguestfs.org/download/

Rich.

-- 
Richard Jones
Red Hat


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] compiling C library wrapper
  2009-11-07 12:25     ` Richard Jones
@ 2009-11-07 16:34       ` Aaron Bohannon
  0 siblings, 0 replies; 5+ messages in thread
From: Aaron Bohannon @ 2009-11-07 16:34 UTC (permalink / raw)
  To: Richard Jones; +Cc: David Allsopp, caml-list

Thanks, I will look into that if I want to distribute wrapper code for
others to use.  My current problems just involve using wrapper code
from other people with incomplete/broken Makefiles.

 - Aaron

On Sat, Nov 7, 2009 at 7:25 AM, Richard Jones <rich@annexia.org> wrote:
> On Thu, Nov 05, 2009 at 06:38:05PM -0500, Aaron Bohannon wrote:
>> Ah!  Yes, that's exactly the part I didn't understand and everything
>> works fine now.  Thank you!
>>
>> I can't say I completely understand the byte-code case, but I don't
>> have an urgent need to.
>>
>> ocamlmklib seems fine, but if it's just a matter of saving keystrokes,
>> "make" does a pretty good job of that (as long as I know what to tell
>> "make" to do).
>
> For the install step, use 'ocamlfind install'.
>
> Grab the latest source tarball for one of our projects,
> eg. libvirt-ocaml, to see how we do it:
>
> http://libvirt.org/sources/ocaml/
> or: http://libguestfs.org/download/
>
> Rich.
>
> --
> Richard Jones
> Red Hat
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-11-07 16:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-05 22:32 compiling C library wrapper Aaron Bohannon
2009-11-05 23:09 ` [Caml-list] " David Allsopp
2009-11-05 23:38   ` Aaron Bohannon
2009-11-07 12:25     ` Richard Jones
2009-11-07 16:34       ` Aaron Bohannon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).