Path: news.weeg.uiowa.edu!news.uiowa.edu!hobbes.physics.uiowa.edu!math.ohio-state.edu!cs.utexas.edu!swrinde!pipex!connect!Tombo Newsgroups: comp.sys.apple2.programmer Subject: Multi-language Programming From: tombo@mail.on-line.co.uk (Richard King) Distribution: world Message-ID: Sender: tombo@mail.on-line.co.uk (Richard King) Organization: On-line Entertainment Date: Fri, 27 May 1994 02:26:49 GMT Lines: 142 Multi-language (Pascal and C) programming WORKS! My brain apparently doesn't - or not very well. However, there are a few things you have to be careful of. In particular, using the #append "xxx" command in C must be handled with care, and {$append 'xxx'} in Pascal must be avoided. Both seem to work, as far as switching from one compiler to another, but it seems that the linker can't handle the task of combining the object-modules, which appear to be properly compiled, into an executable program. A range of strange, incorrect and generally peculiar error-messages seem to be the only result, ranging from perfectly properly-defined variables being reported as undefined, or demands for ..root files which aren't needed (as well as weren't generated and aren't present). *** Calls to Pascal from C To use Pascal routines from a C program, all you need to do is to compile the Pascal code as a UNIT, placing all the externally accessible variables, procedures and functions in the INTERFACE section. Doing so amounts to defining a C-function as non-static. Naturally, in the C program which uses the Pascal code there must be corresponding external declarations, each of which should be provided with the Pascal qualifier - and if there is more than one parameter, the order of them must be reversed in the external C declaration, to allow for the different parameter-passing convention in Pascal. Also, remember that all procedures, functions, and variables in Pascal will appear to the linker in UPPERCASE, and they must be defined in C in uppercase. The #append directive can be used, but the Pascal file which is appended *must* have a {$keep 'xxxx'} directive, and the keep-name must be different from the name of the C-part of the program. Naturally, the link-line must specify both parts, which means that you cannot use 'cmpl' or 'cmplg' to build and run the complete program. You have to do it in two separate steps. *** Calls to C from Pascal To use a C-function from a Pascal program, the process is a little simpler. Essentially, you can forget all the complexities in the C-code, and just compile it as though it were to be used in a pure-C program. You don't even need to apply the Pascal qualifier, provided you remember to deal correctly with the parameter-passing convention. Adding a small 'glue' routine is a good idea, though - it won't take up much space, or cost much time unless it's called *very* frequently, and since reversed parameter bugs are extremely hard to spot and totally devastating to the execution of the program (it's an excellent way to really mess *everything* up) it well be well worth the trouble. In the Pascal program, all you need to do is to define the C-function as external, and from then on you can treat it as though it were a perfectly ordinary Pascal routine. *** In summary. The use of the {$append 'xxxx'} directive in Pascal is to be avoided. As stated, the compilation appears to proceed correctly, but the linkage process falls over. Using 'noroot' #pragma or the 'keep' #pragma doesn't help, either, because the 'keep' is disregarded, not being at the start of the program. The system apparently does not consider switching from the Pascal compiler to the C compiler to be worthy of zeroing the 'keep-name' - and since it's a #pragma, the XJ11 ANSI standard has nothing to say on the matter. The keep-name status *is* zeroed out when #append causes a switch from the C compiler to the Pascal compiler, and thus the {$keep 'xxxx'} directive works correctly. *** Alternatively Most of these difficulties can be avoided (except for the parameter-passing and use of U/c in Pascal) if such routines are placed in a library, in which case 'appending' a file in a different language simply doesn't occur. In general, programs combining routines written in multiple languages will use this approach. *** Modula-2 I haven't really begun to use Modula-2 in anger, so I haven't tried combining C-program and Modula routines, or the opposite, nor Pascal program and Modula-2 routines and the opposite of that, but it would seem likely that the same cautions would apply. When I have done more tests, I'll post the results. *** Demonstration I know it isn't customary to hang BSQs on the end of postings, but since it's not very big, I've appended the test-programs that I have found to work properly, complete with 'build' files. FiLeStArTfIlEsTaRt ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789() ITests.SHK AgwcAAAACA(4GIQg8uLA7KQFCUBvAgwcAwy9 GVvTlzW6G4AnAAAACUhHEohXgYAMeJQFBQgGAEgBAAAAAAAAzBAAAAACAAAAAAAA GVvT)iD2DAAPAMAABAAAAoDAAAw4AALAGAAAAAAAAAQAeJgAAQAGJAABa4lAGAAB CUhHEohXAYAAAAAAAAwAAAAALAAAAAAAAAAIAAAABAAAAAAAAAAAAgMACAAAAAAA 9CAAAwoYMCAAAAAAlR3Q6Q3cpVnYAQGbAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAA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lQDj5WdvlGdjlgb1Z2XoMmb6ADculGInVGdpIXZpBiOlRnbyV2Z lByOlRHeh5mcNsDbmlQDj5WdvlGdwlgb1Z2XoMmb6EDculGInVGdpIXZpBiOlRnb yV2ZJ0wOnVmYN4WawlQC1Z2XgMmbg0jOgEDcyAiKJ0wOk5WZN0wOnVmYN4WaxYXC 9oDI7EDI3lQD0lmcuxWZoNGKxgicpkiMJ0wOpJ3dsVGdngibgQXQlhGd0NHI0JXY xYHIg0DIgwyJpEjdJ0wOgEjdg0jOm91Yj5WdxYHKNsTKydXClRXao4GbmF0JyVGd fNGIuVnZ2ByY9ASMscCIxYHINsTKxYXC9oDIfBHIuVnZ2hyY7kSM3lQD0lmcuxWZ BdCKlRnZwBic1Z2XgMmbgEjdnASP2BCL7kSMuVWDg4CZgACIgACIgACIgACIgACI N0AIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Acf2 ---------------------------------------------------------------------- Obey little, question much - Walt Whitman | The only thing my GS hasn't Specialisation is for ants - Lazarus Long | got is a go-faster stripe. ---------------------------------------------------------------------- Newsgroups: comp.sys.apple2.programmer Path: news.weeg.uiowa.edu!news.uiowa.edu!uunet!MathWorks.Com!news2.near.net!yale!gumby!wupost!waikato!comp.vuw.ac.nz!actrix.gen.nz!dempson From: dempson@actrix.gen.nz (David Empson) Subject: Re: Multi-language programming Message-ID: Organization: Actrix Information Exchange References: Date: Fri, 27 May 1994 13:58:23 GMT Lines: 66 In article , Richard King wrote: > Is anyone able to explain to me why the appended program will not link > properly under Orca? It compiles perfectly happily, but when I try to > link it, I get a report that V1 is an undefined reference in _PASMAIN. OK, I've had a little play and looked at the object files with DUMPOBJ. The problem is as follows: When you use #append or {$append}, the second compiler is called up and is asked to create an object file with the same base name as the original source file. For example, if your files were called test1.pas and test2.cc, then the C compiler would be asked to create an object file with base name 'test1'. The Pascal compiler has already created 'test1.root' and 'test1.a'. The C compiler notes that the '.a' extension is already used, so it creates its object file as 'test1.b'. The linker then links together the three object files. It grabs the startup code from test1.root, P_FUNC and ~_PASMAIN from test1.a, and C_FUNC from test2.a. The problem with the global variable arises because BOTH the Pascal and C compilers output a ~GLOBALS segment to their respective object files. The linker thinks that a partial compilation has been used (because there are '.b' and '.a' object files), so it grabs what it thinks is the "newest" version of the ~GLOBALS segment, which is the one created by the C compiler. This is an empty segment, and you end up with no v1 variable. (You could get around the problem in this case by declaring the variables in BOTH programs, but this may not be practical in all cases.) I cannot see any way around this except to avoid using the append directive. The program works fine if you compile each module separately and link them together when you've finished. The problem doesn't arise when linking assembly language with C or Pascal, unless you happened to use the same name for one of your assembly language segments. There is no problem using #append or {$append} with a file of the same language, because the compiler treats it as an include directive, and will not do a separate compilation. The only way to fix this permanently would be to modify the Pascal and C compilers to use different segment names for their global variables (~PASGLOBALS and ~CGLOBALS for example). I haven't tried mixing Modula-2 with Pascal or C, so the same problem might arise. (The Modula-2 compiler was written by a different author, so it is possible that he chose different names for the object segments.) By the way: when mixing Pascal and C code, it is probably safest to be using compatable versions of the compilers: don't mix a 1.x C compiler with a 2.x Pascal compiler, or vice versa. ORCA/Modula-2 1.0 is compatible with ORCA/C and ORCA/Pascal 2.x. -- David Empson dempson@actrix.gen.nz Snail mail: P.O. Box 27-103, Wellington, New Zealand