Jump to content
Excelsior Forums
oneelkruns

Calling Modula-2 from C under Linux

Recommended Posts

I am trying to call a Modula-2 procedure from a C program under Linux (Fedora B).  I have this as the C code:

#include <stdio.h>

void helloworld_hello();  // nm says this is the procedure name

int main() {

    helloworld_hello();

    return 0;

}

and a simple hello world module:

DEFINITION MODULE helloworld;

PROCEDURE hello;

END helloworld.

and:

IMPLEMENTATION MODULE helloworld;

FROM InOut IMPORT WriteString, WriteLn;

PROCEDURE hello;

BEGIN

    WriteString('Hello world');

    WriteLn;

END hello;

BEGIN

END helloworld.

First I compile the module:

xc =c helloworld.def

xc =c helloworld.mod

which produces a .o and then:

gcc qqq.c helloworld.o /usr/local/xds/lib/x86/libxds.a -lm -g

which produces a.out.  If I run a.out, I get a seg fault.  gdb shows the backtrace as:

Program received signal SIGSEGV, Segmentation fault.

0x0804f038 in IOChan_TextWrite ()

(gdb) bt

#0  0x0804f038 in IOChan_TextWrite ()

#1  0x0804969e in STextIO_WriteString ()

#2  0x08048fdf in helloworld_hello () at helloworld.mod:7

#3  0x08048fba in main () at qqq.c:6

(gdb)

So, finally, my questions:

1. C seemed happy with building the executable, what then is the problem in IOChan_TextWrite()?

2. Is this even the way to call Modula-2 from C?  (I cannot call C from Modula-2 for my application, it must be from C)

Any help appreciated!

Ron

Share this post


Link to post
Share on other sites

It is perfectly possible to call Modula-2 from C, but you need to initialize the Modula-2 runtime and the module you are using. Check out the respecitve sample.

Share this post


Link to post
Share on other sites

It is perfectly possible to call Modula-2 from C, but you need to initialize the Modula-2 runtime and the module you are using. Check out the respecitve sample.

Where might I find this example?  All I see in the XDS documentation is calling C from Modula-2.  Or, do I need to use XDS-C to produce C output instead of object code?

Share this post


Link to post
Share on other sites

Where might I find this example?  All I see in the XDS documentation is calling C from Modula-2.  Or, do I need to use XDS-C to produce C output instead of object code?

In the samples.txt file in the XDS ReadMe folder it says:

The following samples are available in XDS-C only:

        mixed    - mulitlanguage (Modula-2 and C) samples

Share this post


Link to post
Share on other sites
The following samples are available in XDS-C only

Hhmmm.. The file cmain.c in the native Linux distribution is an example of how to call a Modula-2 function from C after first initializing the RTL and the module (as indicated by Snowman).  There is an error in cmain.c:  Replace "m2test.h" with "ctest.h" on line 7.  After that, it works fine on Ubuntu 9.03.  (Note that there is a comment in cmain.c that you cannot use MSVC.)

Share this post


Link to post
Share on other sites

Hhmmm.. The file cmain.c in the native Linux distribution is an example of how to call a Modula-2 function from C after first initializing the RTL and the module (as indicated by Snowman).  There is an error in cmain.c:  Replace "m2test.h" with "ctest.h" on line 7.  After that, it works fine on Ubuntu 9.03.  (Note that there is a comment in cmain.c that you cannot use MSVC.)

Just tried this before seeing your post.  It gave me what I needed to get my own example working.  Sweet!  Thanks for the help everyone.

Ron

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×