Recent

Author Topic: [x86_64][Linux]GCC and FPC compatibility  (Read 4632 times)

Andru

  • Full Member
  • ***
  • Posts: 112
[x86_64][Linux]GCC and FPC compatibility
« on: December 11, 2010, 08:27:22 pm »
I have a problem. I can't get to work correctly my application with gcc-compiled library.
Here the code of library:
Code: [Select]
#include <stdio.h>

typedef struct
{
  double x;
  double y;
} Vect;

void test( Vect a, Vect b, double r )
{
  printf( "% .2f, % .2f\n", a.x, a.y );
  printf( "% .2f, % .2f\n", b.x, b.y );
  printf( "% .2f\n", r );
}

void test2( double ax, double ay, double bx, double by, double r )
{
  printf( "% .2f, % .2f\n", ax, ay );
  printf( "% .2f, % .2f\n", bx, by );
  printf( "% .2f\n", r );
}
Here the code of application:
Code: [Select]
program demo;

type
  Vect = record
    x : Double;
    y : Double;
  end;

function dlopen ( Name : PChar; Flags : longint) : Pointer; cdecl; external 'dl';
function dlclose( Lib : Pointer) : Longint; cdecl; external 'dl';
function dlsym  ( Lib : Pointer; Name : Pchar) : Pointer; cdecl; external 'dl';

var
  a, b    : Vect;
  r       : Double;
  test    : procedure( a : Vect; b : Vect; r : Double ); cdecl;
  test2   : procedure( ax : Double; ay : Double; bx : Double; by : Double; r : Double ); cdecl;
  libtest : Pointer;
begin
  libtest := dlopen( './a.out', $001 );
  test := dlsym( libtest, 'test' );
  test2 := dlsym( libtest, 'test2' );

  a.x := 10;
  a.y := 10;
  b.x := 100;
  b.y := 100;
  r   := 1000;
  test( a, b, r );
  writeln( '==========' );
  test2( a.x, a.y, b.x, b.y, r );
end.

Commands for compilation are:
Code: [Select]
gcc -shared lib.c -O3 -s -fPIC
Code: [Select]
fpc demo.pas -O3 -Xs -Sd

Result:
Quote
1000.00,  0.00
 0.00,  0.00
 0.00
==========
 10.00,  10.00
 100.00,  100.00
 1000.00

FreePascal version: 2.4.2
GCC version: 4.5.1 and 4.4.4

What have I missed? Some options for compilers? Or this is a bug of FreePascal? The same code compiled in 32-bit mode works fine.
« Last Edit: December 11, 2010, 09:01:28 pm by Andru »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12721
  • FPC developer.
Re: [x86_64][Linux]GCC and FPC compatibility
« Reply #1 on: December 13, 2010, 01:52:45 pm »
Try {$packrecords C} before the record declaration. It sets record alignment

The loadlibrary functions are already in unit dynlibs in a portable way (the way you do it now is Linux specific, and using dynlibs hides the minimal ifdefs needed for FreeBSD and OS X/Darwin)

 

TinyPortal © 2005-2018