[m-users.] How do I use a module's main/2 in another module's main/2?

Julian Fondren jfondren at minimaltype.com
Fri Jul 12 18:31:22 AEST 2019


Hello!

Consider two modules, hello.m:

   :- module hello.
   :- interface.
   :- import_module io.
   :- pred main(io::di, io::uo) is det.
   :- implementation.

   main(!IO) :-
   	io.write_string("Hello, world!\n", !IO).

and hello_repeatedly.m:

   :- module hello_repeatedly.
   :- interface.
   :- import_module io.
   :- pred main(io::di, io::uo) is det.
   :- implementation.
   :- use_module hello.

   main(!IO) :-
       hello.main(!IO),
       main(!IO).

When I compile the second as an application, I get an error:

   $ mmc --make hello_repeatedly
   Making Mercury/int3s/hello_repeatedly.int3
   Making Mercury/int3s/hello.int3
   Making Mercury/ints/hello.int
   Making Mercury/ints/hello_repeatedly.int
   Making Mercury/cs/hello.c
   Making Mercury/cs/hello_repeatedly.c
   Making Mercury/os/hello.o
   Making Mercury/os/hello_repeatedly.o
   Making hello_repeatedly
   ** Error making `hello_repeatedly'.
   /usr/bin/ld: Mercury/os/hello_repeatedly.o: in function `<predicate 
'main'/2 mode 0>':
   hello_repeatedly.c:(.text+0x10): multiple definition of `<predicate 
'main'/2 mode 0>'; Mercury/os/hello.o:hello.c:(.text+0x10): first 
defined here
   collect2: error: ld returned 1 exit status

I've also tried

   $ mmc --no-main -c hello
   $ mmc --make-short-int --no-main hello.m
   $ mmc --make-priv-int --no-main hello.m
   $ mmc --make-int --no-main hello.m
   $ mmc hello_repeatedly.m hello.m
   /usr/bin/ld: hello.o: in function `<predicate 'main'/2 mode 0>':
   hello.c:(.text+0x10): multiple definition of `<predicate 'main'/2 mode 
0>'; hello_repeatedly.o:hello_repeatedly.c:(.text+0x10): first defined 
here
   collect2: error: ld returned 1 exit status

and some variations of that.

Is hello_repeatedly currently possible, without altering hello.m?

This is with

   Mercury Compiler, version rotd-2019-06-13, on x86_64-pc-linux-gnu


More information about the users mailing list