[mercury-users] shared library

Fergus Henderson fjh at cs.mu.OZ.AU
Sun Jan 18 17:23:06 AEDT 2004


On 16-Jan-2004, Marcus Vinicius Santos <m3santos at scs.ryerson.ca> wrote:
> I would like to call mercury predicates from another language (Pliant,
> for instance).
> 
> To test that, I have created the following simple mercury program:
> 
> :- module hello.
> :- interface.
> :- pred test is det.
> 
> :- implementation.
> 
> test.
> 
> And compiled this program as a shared library, as follows:
> 
> mmc --pic-reg -c hello.m
> ml --make-shared-lib -o libhello.so hello.o
> 
> The Pliant compiler seems not able to find the predicate 'test' in the
> shared library.

> I tried to link a similar program in C (below), compiling it with gcc, and
> it works. Pliant does find the function in the library and runs it.

If you try a similar program in C++, I'll bet that it doesn't work either.
To make that program work in C++, you need to explicitly declare the function
to have C linkage.  In Mercury it is similar: you need to explicitly use a
"pragma export" declaration, e.g.

	:- pragma export(test, "test").

There are several reasons why this is necessary.

The first reason is that the Mercury compiler normally generates code
which uses a different calling convention than C.

The second reason is that Mercury permits overloading on arity (number
of arguments).  So there might be more than one predicate named "test".
Also, there could be both a Mercury predicate named "test" and a Mercury
function named "test".  Each of these could also have more than one mode.
Each different mode corresponds to a different C function with a different name.

A "pragma export" declaration tells the Mercury compiler to generate
a wrapper function for a particular mode of a particular predicate
or function.  The wrapper function uses the normal C calling conventions,
rather than the Mercury calling conventions, and has the exact name specified
in quotes in the "pragma export" declaration.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list