[mercury-users] Addendum to: need help: Mercury+C/C++?

Tyson Dowd trd at cs.mu.OZ.AU
Wed Sep 2 12:52:21 AEST 1998


On 01-Sep-1998, tcklnbrg <at at ingenuity-sw.com> wrote:
> tcklnbrg wrote:
> > The lexer.c file contains one global var, "char * tokenStr",
> 
> Addendum:  The above is wrong:  it is "string * tokenStr"
> 
> This means that lexer.c, and presumably et.al. 
> must be compiled with a c++ compiler,
> in this case g++ rather than gcc.  The "string", above is the
> g++/std/ string.

Ok, that's fine, we can interface to C++ using C calls.

You will need to link using g++ instead of gcc, so your Mmakefile needs

ML = MERCURY_C_COMPILER=g++ ml

Depending on your g++ version (2.7.2 has bugs), you might need to add

HACK=	-DALPHA_REGS_H \
	-DMIPS_REGS_H \
	-DSPARC_REGS_H \
	-DI386_REGS_H \
	-DPA_REGS_H \
	-DRS6000_REGS_H

CXXFLAGS := $(HACK) $(CXXFLAGS) $(EXTRA_CXXFLAGS)

And lastly, you need to make sure you use the "create lexer.h and
#include it from Mercury *and* lexer.cc" solution
from my previous email, but put this in lexer.h:

#ifdef __cplusplus
extern "C" {
#endif

extern char *tokenize(char *);

#ifdef __cplusplus
}
#endif

(This is the normal code for interfacing C with C++.  If you do not
do this, you will get link errors because C and C++ mangle symbol
names differently unless they are told they are interoperating).

If tokenize returns a "string" not a char *, you will need to write
a bit of C code that calls tokenize and converts the result to a
char * string (usually C++ string classes overload assignment so
this is pretty easy to do).

> 
> I have tried editing the .dep to set the paths correctly and all
> I manage to do is change the error messages.  They are always about
> some something undefined or not found.

It's not worth editing the .dep file except for very quick once-off fixes
because Mercury will just regenerate it at some stage (e.g. mmake
depend) and you will lose your changes.

We could do with a little better support for linking C++ code with
Mercury.

-- 
       Tyson Dowd           # There isn't any reason why Linux can't be
                            # implemented as an enterprise computing solution.
     trd at cs.mu.oz.au        # Find out what you've been missing while you've
http://www.cs.mu.oz.au/~trd # been rebooting Windows NT. -- InfoWorld, 1998.



More information about the users mailing list