Page 1 of 1

pascal ?

Posted: Fri May 28, 2010 9:28 pm
by Sam111
I am just learning another language other then my standard c/c++ , asm compiled languages I know.
I am getting familar will fortran, and pascal.

My problem is I am trying to call pascal from c. I know pascal uses a right to left calling convention where the callee cleans the stack. That is why I am using stdcall and having a wrapper function reverse the function parameters the the pascal function.

My problem in getting this to work is from the pascal side of things the structure is

Code: Select all

program  testfunc (output);
function  ADD_TWO ( value1, value2 : integer ) : integer;
	begin
	     ADD_TWO := value1 + value2 ;
	end;

begin;
{do nothing in here this is the equivalent of main in pascal}
end.
My problem is when I compile

Code: Select all

gpc -c testfunc.pas  (this gives me testfunc.o which is what it should)
gcc test.c testfunc.o ( this gives me 2 errors )
errors are

Code: Select all

testfunc.o: In function `main':
testfunc.pas:(.text+0x93): multiple definition of `main'
/tmp/cc9VGadq.o:test.c:(.text+0x0): first defined here
/tmp/cc9VGadq.o: In function `main':
test.c:(.text+0x2d): undefined reference to `ADD_TWO'
collect2: ld returned 1 exit status

What I think is the problem is that

Code: Select all

begin;
end.
in the testfunc.pas file creates a main entry point as does my c file.
So is their away to just declare function with no main part in a .pas file I don't know how to create .pas file that holds just functions/procedures all that I have seen on the internet is examples with the main in them.
So first off if anybody can fix this entry point issue that would be a great help.


error 2 is probably caused by a name mangleing thing I have read that pascal compiles their functions with a underscore infront of them ? Either way I don't know of a good way to fix this yet?
Like in asm on a windows machine I have to call a c function with an underscore in front of it but that is an easy fix just but the underscore on it. For pascal I don't know how to do the equivalent if that is the problem.


Here is my c code I am using the calling convention stdcall for my extern pascal function call from c program

Code: Select all

#include <stdio.h>

extern void __attribute__((stdcall)) ADD_TWO( int a , int b );

int main(int argc , char **argv) {

  puts("Going to call Pascal!");
  ADD_TWO( 1 , 2 ) ;
  puts("Returned from Pascal!");

  return 0;
}


Thanks for any help

Re: pascal ?

Posted: Sat May 29, 2010 2:11 am
by Combuster
So is their away to just declare function with no main part in a .pas file I don't know how to create .pas file that holds just functions/procedures all that I have seen on the internet is examples with the main in them.
Have you even bothered to try the obvious - remove the "main" block?

Re: pascal ?

Posted: Sat May 29, 2010 2:17 am
by gerryg400
So is their away to just declare function with no main part in a .pas file I don't know how to create .pas file that holds just functions/procedures all that I have
I've never actually written a pascal program, but don't you create a 'unit', rather than a 'program', if you don't want to have a main function ? I don't know the syntax but I'll bet you need to remove the 'program' keyword.

Re: pascal ?

Posted: Sat May 29, 2010 3:43 am
by Velko
Here's an example for Pascal "unit":

Code: Select all

unit testfunc;

interface

function  ADD_TWO ( value1, value2 : integer ) : integer;

implementation

function  ADD_TWO ( value1, value2 : integer ) : integer;
begin
        ADD_TWO := value1 + value2 ;
end;

end.

Re: pascal ?

Posted: Sat May 29, 2010 7:45 am
by Love4Boobies
Haha, nice thread :lol:

Re: pascal ?

Posted: Sat May 29, 2010 8:52 am
by Kevin
With FPC (which is a much more popular Pascal compiler than gpc, as far as I can tell), you would do it like this (copied from one of my kernels):

Code: Select all

procedure cdi_init; cdecl; public; alias: 'cdi_init';
begin
    // ...
end;
cdecl is for using the C calling convention, and the alias makes sure that the name isn't mangled. I'm sure gpc provides something similar. Google suggests attribute(cdecl); for the calling convention.

And yes, you want a unit, not a program.

Re: pascal ?

Posted: Sat May 29, 2010 1:12 pm
by Sam111
Thanks
I was new to pascal and didn't know how to make the equivalent of a function file where you can put all your procedures/functions in and not have the main entry point in.

So is the unit /interface commands in pascal for making your own function library/file that you can call with a using command.

And when you define a .pas file to be a program then it must have a main entry point.

If that is the case then usually you would have tons of units/interfaces .pas files and only one program .pas file for a large pascal project.

Anyway I haven't got the syntax correct for the cdecl call , and the mangled problem yet..... I will post back if their is any issues with it (I don't think their will be though seems pretty straight forward now )

Is their anything else I have to set so the pascal function can be called from c.
I know in my c program I have it as extern but for the pascal function do I also have to declare it global or some other command I need that I am not aware of in the pascal language?

I was think that telling c that it is an external function and telling pascal that it must name the function the exact name no prefix's as well as telling it to use the default c calling convention would be enough.
But when I call asm from c I also have to tell the function to be global in the asm code... wondering if anything similar has to be done in the pascal code.

Thanks

Re: pascal ?

Posted: Sat May 29, 2010 2:19 pm
by Kevin
Sam111 wrote:Thanks
If that is the case then usually you would have tons of units/interfaces .pas files and only one program .pas file for a large pascal project.
Right. And often this program .pas file is very short and just calls a function in some unit that does the real work.
I know in my c program I have it as extern but for the pascal function do I also have to declare it global or some other command I need that I am not aware of in the pascal language?

I was think that telling c that it is an external function and telling pascal that it must name the function the exact name no prefix's as well as telling it to use the default c calling convention would be enough.
But when I call asm from c I also have to tell the function to be global in the asm code... wondering if anything similar has to be done in the pascal code.
This is completely compiler specific. I could tell you for FPC, but with gpc things might look completely different. From a Pascal perspective, types, variables, functions etc. are considered public if they are declared in the interface section. If you have them only in the implementation section, you could compare them to static functions in C.