C calling FPC pascal, linker issues 
Author Message
 C calling FPC pascal, linker issues

I'm trying to link a large chunk of older Pascal code into a C simulation
framework on a windows 2000 system.  The framework makes the pascal code
(now compiled by FPC) believe it is executing in its native environment (an
embedded computer).  I added the library path for system.ow which resolved
references for ABS() for example.  This example will show the remaining
issues.

/* Start of C Source: */

        #include <stdio.h>

        short MyABS(short A);

        void main(void)
        {
                 short A = -20;
                 short B = MyABS(A);
                 printf("A => %d, B => %d\n", A, B);
        }
/* End of C Source */

{ Start of Pascal Source: }
        UNIT Test1;
        Interface
        Implementation

                function MyAbs(A : integer) : INTEGER; CDECL; EXPORT;
                begin
                         MyAbs := ABS(A);
                end;
        end.
{End of Pascal Source}

The linker generates the following errors:
        system.ow : warning LNK4078: multiple ".idata" sections found with
different attributes (40000000)
        system.ow : error LNK2001: unresolved external symbol INITFINAL
        system.ow : error LNK2001: unresolved external symbol HEAP
        system.ow : error LNK2001: unresolved external symbol HEAPSIZE
        system.ow : error LNK2001: unresolved external symbol PASCALMAIN

I suspect the 1st warning has to do with the compiler settings (packing of
records and enums).
I couldn't find the following 4 symbols.  I suppose I could write my own ABS
function in C.

Jim Whitmire



Sat, 08 Oct 2005 21:44:15 GMT  
 C calling FPC pascal, linker issues

Quote:

> I'm trying to link a large chunk of older pascal code into a C simulation
> framework on a windows 2000 system.  The framework makes the pascal code
> (now compiled by FPC) believe it is executing in its native environment (an
> embedded computer).  I added the library path for system.ow which resolved
> references for ABS() for example.  This example will show the remaining
> issues.

The problem is that FPC has two ways of linking. One with external C, and one without, but
both assume a _PASCAL_ main program.

The problem, to fix it the otherway around, you either have to patch your C
runtime, or try to emulate the missing parts from C. (which will probably
turn out to create a "special" pascal or C system unit)

Quote:

> The linker generates the following errors:
>         system.ow : warning LNK4078: multiple ".idata" sections found with
> different attributes (40000000)

I think this means that both the FPC and the C mainprogram reserve a segment
for a heap.

Quote:
>         system.ow : error LNK2001: unresolved external symbol INITFINAL

This probably is the finalisation-code of the system unit

Quote:
>         system.ow : error LNK2001: unresolved external symbol HEAP
>         system.ow : error LNK2001: unresolved external symbol HEAPSIZE

Variables for heapmanager defined in the adapted startup code.

Quote:
>         system.ow : error LNK2001: unresolved external symbol PASCALMAIN

This is the Pascal Main program, which you indeed don't have.

Quote:
> I suspect the 1st warning has to do with the compiler settings (packing of
> records and enums).

No. Multiple heaps.

Quote:
> I couldn't find the following 4 symbols.  I suppose I could write my own ABS
> function in C.

The easiest is to move the C main program to a cdecl function, and call that from a normal
FPC program that links to C (instead of a basically C program that links to Pascal)

The other solution is to adapt the startup code (cprt0.o or crt0.o probably, but I
don't know Windows that well), or write a custom system unit.



Sat, 08 Oct 2005 22:26:41 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. FPC v1.0 linux: Linker??

2. linker problem in gnu pascal

3. FPC:problem with printing from fpc programs

4. FPC: translate execvp from c to FPC

5. GREEN HILLS OASYS LINKER 5.17

6. T.A.S.M. & T.L.I.N.K. Turbo Linker e Assembler

7. How smart is the linker?

8. How smart is the linker?

9. Help With Pascal Iwant to call C function from Pascal

 

 
Powered by phpBB® Forum Software