[mercury-users] Using Mercury code in C projects

Ondrej Bojar oboj7042 at ss1000.ms.mff.cuni.cz
Thu Apr 4 06:58:59 AEST 2002


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

I would like to compile and link this way:

module.m  ----mmake module.o---> module.o, module.h (as the module exports
                                                     some functions)

And later on: (my colleague's Makefile, not Mmakefile)

cfile.h:   #include "module.h"

gcc -c cfile.c   ----------> cfile.o

main.c: #include "cfile.h"

gcc -o main cfile.o module.o main.c  -----> main

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

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. The .c
files are expected to be strict C (for example C++ style comments // are
not understood), in the .cc files, <stdio.h> is included, but with such
options, that for instance snprintf is undeclared, even the output of
bison won't compile.

I tried to change the options to make it compile, I tried to rename .c
files to .cc (which helps a lot :-), but I still didn't reach the end.

I got as far as possible, when I compiled all his own .c and .yy and .cc
and whatever with his own gcc/g++ to get corresponding .o's, with the
very exception for:

the single cscript.cc that is to call my mercury funcion
and the main.cc that is to be compiled and linked with mercury objects and

For both these files I call mgnuc with the same gradeflags
(--high-level-code) that were used to produce the module.o and no other
special options. This succeeds.

However, the linker reports undefined reference to the exact c++ function
that is to call my mercury code, stored in the exact exceptional file

(Indirect) call of the linker:

mgnuc --high-level-code -I../../../..//src/ent/lingv2 -lm -I ./ -I
../../../..//src/shared -I ../../../..//src/ent/milan -Wall -Wtraditional
-Winline -g3 -ggdb  Temporary/main.o Temporary/atinstr.o Temporary/debug.o
Temporary/stack.o Temporary/queue.o Temporary/cscript.o
Temporary/identify.o Temporary/lex.yy.o Temporary/compound.o
Temporary/scripts.o Temporary/simple.o Temporary/planner.o
../../../..//src/ent/milan/veta.o ../../../..//src/ent/milan/db.o
../../../..//src/ent/milan/seznam.o ../../../..//src/ent/milan/extra.o -o
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 &)'

In fact, in this attempt, I even didn't try to actually call my mercury
function, I just tried to compile it...

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?

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?

To be honest, my "module in Mercury" is in fact a huge set of modules with
cross links and even cross links to another C++ code, but itself, with a
main module written in Mercury, it works just fine.
I also tried to go on this way -- to simply swallow my colleague's code in
my Mercury main, but the problem with undefined reference to
`Cfunction...' remains.

Thanks for any help,

-------------- next part --------------
A non-text attachment was scrubbed...
Name: errorlog.txt.gz
Type: application/x-gzip
Size: 7260 bytes
URL: <http://lists.mercurylang.org/archives/users/attachments/20020403/3323fa7a/attachment.bin>

More information about the users mailing list