Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-05

Re: Problems linking FPC-built library into C program on linux x86_64

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michael Van Canneyt
to Dominique Leducq: I still don't succeed in linking Free Pascal compiled code with a C program. When linking…
Dominique Leducq wrote 33 lines
Hi again, I still don't succeed in linking Free Pascal compiled code with a C program. When linking with the shared library built by fpc, I have problems with undefined symbols, which are defined in the loaders. $ gcc -o test_shared test2.o -L. -lsoTest3 ./libsoTest3.so: undefined reference to `__stkptr' ./libsoTest3.so: undefined reference to `_haltproc' ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argc' ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argv' collect2: ld returned 1 exit status When linking in the loader prt0.o too, there are symbol conflicts with /usr/lib/crt1.o (_start, __data_start) if I call ld through gcc. $ gcc -o test_shared test2.o prt0.o -L. -lsoTest3 prt0.o: In function `_start': (.text+0x0): multiple definition of `_start' /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:../sysdeps/x86_64/elf/start.S:65: first defined here prt0.o: In function `data_start': (.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:(.data+0x0): first defined here collect2: ld returned 1 exit status Exactly the same happens when linking statically. If I call ld directly, linking succeeds, but the main() of the C program never gets called... However, according to the FPC Programmer's Guide, "using a pascal library from a C program" (12.4) seems to be supported... Do I miss something ?

That statement means that you can create a separate library, which can be called from a C program.

Doing what you want is to include pascal object files directly in a C program. That is only possible if you create your own loader stub.

Michael.

Reply by email

Dominique Leducq

Hi again,

I still don't succeed in linking Free Pascal compiled code with a C program.

When linking with the shared library built by fpc, I have problems with undefined symbols, which are defined in the loaders.

$ gcc -o test_shared test2.o -L. -lsoTest3
./libsoTest3.so: undefined reference to `__stkptr' ./libsoTest3.so: undefined reference to `_haltproc' ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argc' ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argv' collect2: ld returned 1 exit status

When linking in the loader prt0.o too, there are symbol conflicts with /usr/lib/crt1.o (_start, __data_start) if I call ld through gcc.

$ gcc -o test_shared test2.o prt0.o -L. -lsoTest3
prt0.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:../sysdeps/x86_64/elf/start.S:65: first defined here prt0.o: In function `data_start':
(.data+0x0): multiple definition of `__data_start' /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:(.data+0x0): first defined here collect2: ld returned 1 exit status

Exactly the same happens when linking statically.

If I call ld directly, linking succeeds, but the main() of the C program never gets called...

However, according to the FPC Programmer's Guide, "using a pascal library from a C program" (12.4) seems to be supported...

Do I miss something ?

Thx for your help,

Dominique Leducq

Reply by email

Dominique Leducq
to Michael Van Canneyt: That statement means that you can create a separate library, which can be called from a C…
Show quoted text 41 lines
On Wed, 24 May 2006, Dominique Leducq wrote: > Hi again, > > I still don't succeed in linking Free Pascal compiled code with a C program. > > When linking with the shared library built by fpc, I have problems with undefined symbols, which are defined in the loaders. > > $ gcc -o test_shared test2.o -L. -lsoTest3 > ./libsoTest3.so: undefined reference to `__stkptr' > ./libsoTest3.so: undefined reference to `_haltproc' > ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argc' > ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argv' > collect2: ld returned 1 exit status > > When linking in the loader prt0.o too, there are symbol conflicts with /usr/lib/crt1.o (_start, __data_start) if I call ld through gcc. > > $ gcc -o test_shared test2.o prt0.o -L. -lsoTest3 > prt0.o: In function `_start': > (.text+0x0): multiple definition of `_start' > /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:../sysdeps/x86_64/elf/start.S:65: first defined here > prt0.o: In function `data_start': > (.data+0x0): multiple definition of `__data_start' > /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:(.data+0x0): first defined here > collect2: ld returned 1 exit status > > > Exactly the same happens when linking statically. > > > If I call ld directly, linking succeeds, but the main() of the C program never gets called... > > However, according to the FPC Programmer's Guide, "using a pascal library from a C program" (12.4) seems to be supported... > > Do I miss something ? That statement means that you can create a separate library, which can be called from a C program.

That is actually what I want to do. How can I achieve this ? I followed the steps in the Programmer Guide, to no avail.

Doing what you want is to include pascal object files directly in a C program. That is only possible if you create your own loader stub. Michael.

Reply by email

Michael Van Canneyt
to Dominique Leducq: On Wed, 24 May 2006 15:40:27 +0200 OK, I've done it with this little trick : $ gcc -c…
Dominique Leducq wrote 73 lines
On Wed, 24 May 2006 15:40:27 +0200 Dominique Leducq wrote: > On Wed, 24 May 2006 11:49:03 +0200 (CEST) > Michael Van Canneyt wrote: > >> >> >> On Wed, 24 May 2006, Dominique Leducq wrote: >> >>> Hi again, >>> >>> I still don't succeed in linking Free Pascal compiled code with a C program. >>> >>> When linking with the shared library built by fpc, I have problems with undefined symbols, which are defined in the loaders. >>> >>> $ gcc -o test_shared test2.o -L. -lsoTest3 >>> ./libsoTest3.so: undefined reference to `__stkptr' >>> ./libsoTest3.so: undefined reference to `_haltproc' >>> ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argc' >>> ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argv' >>> collect2: ld returned 1 exit status >>> >>> When linking in the loader prt0.o too, there are symbol conflicts with /usr/lib/crt1.o (_start, __data_start) if I call ld through gcc. >>> >>> $ gcc -o test_shared test2.o prt0.o -L. -lsoTest3 >>> prt0.o: In function `_start': >>> (.text+0x0): multiple definition of `_start' >>> /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:../sysdeps/x86_64/elf/start.S:65: first defined here >>> prt0.o: In function `data_start': >>> (.data+0x0): multiple definition of `__data_start' >>> /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:(.data+0x0): first defined here >>> collect2: ld returned 1 exit status >>> >>> >>> Exactly the same happens when linking statically. >>> >>> >>> If I call ld directly, linking succeeds, but the main() of the C program never gets called... >>> >>> However, according to the FPC Programmer's Guide, "using a pascal library from a C program" (12.4) seems to be supported... >>> >>> Do I miss something ? >> >> That statement means that you can create a separate library, >> which can be called from a C program. >> > > That is actually what I want to do. How can I achieve this ? I followed the steps in the Programmer Guide, to no avail. > OK, I've done it with this little trick : $ gcc -c test3.c $ gcc -o test3 test3.o dummy.o -L. -lsoTest5 with the source of dummy.c: #define NULL (void *)0 long operatingsystem_parameter_argc = 0; char **operatingsystem_parameter_argv = NULL; char **operatingsystem_parameter_envp = NULL; void *__stkptr = NULL; void _haltproc(long e) { return; } When calling a library function that do no IO (only arithmetics on the parameters), it works. When doing simple IO (writeLn), it doesn't: I see calls to handleerror_frame, get_frame, get_caller_frame, get_caller_addr, break_error... when debugging, following calls to write_text_pchar_as_array and IOcheck

Add a call to FPC_INITIALIZEUNITS before calling any FPC routines, and it should work fine.

Michael.

Reply by email

Dominique Leducq
to Dominique Leducq: On Wed, 24 May 2006 11:49:03 +0200 (CEST) That is actually what I want to do. How can I…
Show quoted text 47 lines
On Wed, 24 May 2006 11:49:03 +0200 (CEST) Michael Van Canneyt wrote: > > > On Wed, 24 May 2006, Dominique Leducq wrote: > > > Hi again, > > > > I still don't succeed in linking Free Pascal compiled code with a C program. > > > > When linking with the shared library built by fpc, I have problems with undefined symbols, which are defined in the loaders. > > > > $ gcc -o test_shared test2.o -L. -lsoTest3 > > ./libsoTest3.so: undefined reference to `__stkptr' > > ./libsoTest3.so: undefined reference to `_haltproc' > > ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argc' > > ./libsoTest3.so: undefined reference to `operatingsystem_parameter_argv' > > collect2: ld returned 1 exit status > > > > When linking in the loader prt0.o too, there are symbol conflicts with /usr/lib/crt1.o (_start, __data_start) if I call ld through gcc. > > > > $ gcc -o test_shared test2.o prt0.o -L. -lsoTest3 > > prt0.o: In function `_start': > > (.text+0x0): multiple definition of `_start' > > /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:../sysdeps/x86_64/elf/start.S:65: first defined here > > prt0.o: In function `data_start': > > (.data+0x0): multiple definition of `__data_start' > > /usr/lib/gcc/x86_64-linux-gnu/4.0.3/../../../../lib64/crt1.o:(.data+0x0): first defined here > > collect2: ld returned 1 exit status > > > > > > Exactly the same happens when linking statically. > > > > > > If I call ld directly, linking succeeds, but the main() of the C program never gets called... > > > > However, according to the FPC Programmer's Guide, "using a pascal library from a C program" (12.4) seems to be supported... > > > > Do I miss something ? > > That statement means that you can create a separate library, > which can be called from a C program. > That is actually what I want to do. How can I achieve this ? I followed the steps in the Programmer Guide, to no avail.

OK, I've done it with this little trick :

$ gcc -c test3.c
$ gcc -o test3 test3.o dummy.o -L. -lsoTest5

with the source of dummy.c:

#define NULL (void *)0

long operatingsystem_parameter_argc = 0;
char **operatingsystem_parameter_argv = NULL;
char **operatingsystem_parameter_envp = NULL;
void *__stkptr = NULL;

void _haltproc(long e)
{

  return;

}

When calling a library function that do no IO (only arithmetics on the parameters), it works. When doing simple IO (writeLn), it doesn't: I see calls to handleerror_frame, get_frame, get_caller_frame, get_caller_addr, break_error... when debugging, following calls to write_text_pchar_as_array and IOcheck

Dominique Leducq.

Reply by email

Dominique Leducq
to Michael Van Canneyt: Add a call to FPC_INITIALIZEUNITS before calling any FPC routines, and it should work…
Show quoted text 7 lines
> When calling a library function that do no IO (only arithmetics on the parameters), it works. > When doing simple IO (writeLn), it doesn't: I see calls to handleerror_frame, get_frame, get_caller_frame, get_caller_addr, break_error... when debugging, following calls to write_text_pchar_as_array and IOcheck Add a call to FPC_INITIALIZEUNITS before calling any FPC routines, and it should work fine. Michael.

It works, thanks !

Dominique.

Reply by email