[mercury-users] Using Mercury code in C projects

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Apr 4 17:03:06 AEST 2002

On 03-Apr-2002, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> I'm trying to call mercury functions from a C(++). If I follow the exact
> instructions in Language Reference, it's quite easy. The hard thing is
> that I do not want to use mmake to do the compilation -- (it's my
> colleague's main and I just wrote something for him).

It's probably a bad idea to not use mmake.
Note that you can invoke mmake from a Makefile,
so using mmake doesn't mean you can't use standard make as well.

> Of course, this doesn't work. module.h depends on mercury.h which is
> Idontknowwhere.

It's in <prefix>/lib/mercury/inc, where <prefix> is where you installed
Mercury, e.g. /usr/local/mercury-<version>.

> In the Makefile, tried using mgnuc instead of gcc. This helps enough to
> find mercury.h, but hardly fights with my colleague's code. He has an
> untidy mixture of .c and .cc, and lex and bison. (I have no experience
> with any of them, but I guess they produce a c code, but he inserts
> some cc code in it.)
> He uses generic gcc for .c and g++ for .cc, with no extra options (just
> -W..arnings and -g..debugging).
> Unfortunately mgnuc adds options that make his code uncompilable.

Try using `mgnuc --no-ansi --no-check'.

> However, the linker reports undefined reference to the exact c++ function
> that is to call my mercury code, stored in the exact exceptional file
> cscript.cc.
> Temporary/planner.o: In function `Node::OneOP(Queue &)':
> /afs/ms/uo/oboj7042/enti/src/ent/mikulas/simple/planner.cc:531: undefined
> reference to `Cfunction(unsigned short, unsigned short, unsigned short,
> unsigned long **, Queue &, bool &)'

That one looks like your problem...
maybe it is because you are mixing C and C++ linkage?
i.e. maybe this function is defined in C, rather than C++,
but hasn't been declared as `extern "C"' in the header file?

> If I tried to indeed call my mercury function, I got a heap of undefined
> references to functions in mercury library. (See the attachment.)
> What did I forget to tell the linker?

You need to link in the Mercury libraries.
The simplest way to do this is to use `ml' to do the link.
If you are linking in C++ code, you just need to pass `-lstdc++' to `ml'.

> Is there a way to compile all mercury files, runtime and my modules, into
> a nice package, with 1 header file and 1 library, so it would be easy to
> include and link?

You can compile all your Mercury files into a single library, using
Mmake's support for libraries, i.e. `make <foo>.depend; mmake lib<foo>',
where <foo> is the name of your top-level module, which should import
or contain all the other modules in your library.

However, this package will depend on the Mercury standard library,
rather than being entirely self-contained.

So you do need to pass the appropriate `-I' options to the C or C++ compiler
to tell it where to find the Mercury header files, and you need to use
`ml' to do the link (or pass the appropriate options to the linker
to link in the right libraries for Mercury, but using `ml' is easier).

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