Sign in Join

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

Mailing lists / fpc-pascal / 2014-03

Linking object file

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Darius Blaszyk

Hi,

I was wondering if it's possible to link FPC object files by using the $Link directive? At least I have troubles in getting this to work and therefore I'm asking here. Please consider the minimal example I have attached to this email.

When linking the object file that implements the test_proc procedure I get the following compiler output:

Hint: Start of reading config file
C:lazarusfpc2.6.2binx86_64-win64fpc.cfg
Hint: End of reading config file C:lazarusfpc2.6.2binx86_64-win64fpc.cfg Free Pascal Compiler version 2.6.2 [2014/02/28] for x86_64 Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: Win64 for x64
Compiling test.pp
Linking test.exe
test.pp(8,1) Error: Undefined symbol: TEST_INT_TEST_PROC test.pp(8,1) Fatal: There were 1 errors compiling module, stopping

What is needed in the test_impl.pas file so the linker can find the implemented function? Do I need to create a library and export the function perhaps?

Regards, Darius Blaszyk

Reply by email

Unknown author
to Darius Blaszyk

Darius:

I just went through this and it is fresh in my mind, but with 32 bit code.

I used CodeBlocks with MingGW to compile the c source code to a static library with an mylib.A suffix.

I am pretty sure you cannot use MSVC to compile the c code.

Then use the {$LINKLIB path/mylib.a} directive to link.

You do not need to use individual object files since many times there are many that are combined into a single static library.

When you use codeblocks, you will need to make an adjustment to produce 64 bit binary.

It is just easier to stay with 32 bit if you can.

Let us know how it turns out in 64 bit {$LINKLIB xxxx}

Cheers,

md

Darius Blaszyk wrote 34 lines
Hi, I was wondering if it's possible to link FPC object files by using the $Link directive? At least I have troubles in getting this to work and therefore I'm asking here. Please consider the minimal example I have attached to this email. When linking the object file that implements the test_proc procedure I get the following compiler output: Hint: Start of reading config file C:\lazarus\fpc\2.6.2\bin\x86_64-win64\fpc.cfg Hint: End of reading config file C:\lazarus\fpc\2.6.2\bin\x86_64-win64\fpc.cfg Free Pascal Compiler version 2.6.2 [2014/02/28] for x86_64 Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: Win64 for x64 Compiling test.pp Linking test.exe test.pp(8,1) Error: Undefined symbol: TEST_INT_TEST_PROC test.pp(8,1) Fatal: There were 1 errors compiling module, stopping What is needed in the test_impl.pas file so the linker can find the implemented function? Do I need to create a library and export the function perhaps? Regards, Darius Blaszyk _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Darius Blaszyk
to Unknown author

Hi md,

Linking with C files works just fine. No problem with that. However I would like to link regular FPC object files instead. Please see the three files I attached in my first email. For some reason the linker could not find the implemented procedure. So the question is, how to modify the implementation unit so everything works.

Regards, Darius

schreef op 20 mrt '14:

Show quoted text 51 lines
Darius: I just went through this and it is fresh in my mind, but with 32 bit code. I used CodeBlocks with MingGW to compile the c source code to a static library with an mylib.A suffix. I am pretty sure you cannot use MSVC to compile the c code. Then use the {$LINKLIB path/mylib.a} directive to link. You do not need to use individual object files since many times there are many that are combined into a single static library. When you use codeblocks, you will need to make an adjustment to produce 64 bit binary. It is just easier to stay with 32 bit if you can. Let us know how it turns out in 64 bit {$LINKLIB xxxx} Cheers, md On 3/19/2014 6:49 PM, Darius Blaszyk wrote: > Hi, > > I was wondering if it's possible to link FPC object files by using the $Link directive? At least I have troubles in getting this to work and therefore I'm asking here. Please consider the minimal example I have attached to this email. > > When linking the object file that implements the test_proc procedure I get the following compiler output: > > Hint: Start of reading config file C:lazarusfpc2.6.2binx86_64-win64fpc.cfg > Hint: End of reading config file C:lazarusfpc2.6.2binx86_64-win64fpc.cfg > Free Pascal Compiler version 2.6.2 [2014/02/28] for x86_64 > Copyright (c) 1993-2012 by Florian Klaempfl and others > Target OS: Win64 for x64 > Compiling test.pp > Linking test.exe > test.pp(8,1) Error: Undefined symbol: TEST_INT_TEST_PROC > test.pp(8,1) Fatal: There were 1 errors compiling module, stopping > > What is needed in the test_impl.pas file so the linker can find the implemented function? Do I need to create a library and export the function perhaps? > > Regards, Darius Blaszyk > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [1] _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [1]

Links:
------
[1] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Tomas Hajny
to Darius Blaszyk: I was wondering if it's possible to link FPC object files by using the $Link directive?…

On Wed, March 19, 2014 23:49, Darius Blaszyk wrote:

Hi,

Show quoted text 22 lines
I was wondering if it's possible to link FPC object files by using the $Link directive? At least I have troubles in getting this to work and therefore I'm asking here. Please consider the minimal example I have attached to this email. When linking the object file that implements the test_proc procedure I get the following compiler output: Hint: Start of reading config file C:lazarusfpc2.6.2binx86_64-win64fpc.cfg Hint: End of reading config file C:lazarusfpc2.6.2binx86_64-win64fpc.cfg Free Pascal Compiler version 2.6.2 [2014/02/28] for x86_64 Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: Win64 for x64 Compiling test.pp Linking test.exe test.pp(8,1) Error: Undefined symbol: TEST_INT_TEST_PROC test.pp(8,1) Fatal: There were 1 errors compiling module, stopping What is needed in the test_impl.pas file so the linker can find the implemented function? Do I need to create a library and export the function perhaps?

Basically, you have two options:

1) Provide the import declaration with the mangled name (i.e. the name really stored within the .o file). This may possibly change between different FPC versions, but if you intend to link to an existing object file, you would already know which one was used. In your case, this would mean changing line 7 in test_int.pas to:

procedure test_proc; external name 'TEST_IMPL_TEST_PROC';

(the mangled name may be found either by using -a when compiling test_impl.pas and looking into the generated test_impl.s or e.g. by using 'objdump -t test_impl.o'.

2) Specify your own name during rather than relying on some mangling rules
- change line 7 in test_impl.pas to:

procedure test_proc; export alias: 'My_Test_Proc';

and then line 7 in test_int.pas needs to change accordingly to:

procedure test_proc; external name 'My_Test_Proc';

Tomas

Reply by email

Darius Blaszyk
to Tomas Hajny

Thanks Tomas,

I did a quick test and it works perfectly!

Regards, Darius

Tomas Hajny schreef op 20 mrt '14:

Show quoted text 34 lines
On Wed, March 19, 2014 23:49, Darius Blaszyk wrote: Hi, > I was wondering if it's possible to link FPC object files by using the $Link directive? At least I have troubles in getting this to work and therefore I'm asking here. Please consider the minimal example I have attached to this email. When linking the object file that implements the test_proc procedure I get the following compiler output: Hint: Start of reading config file C:lazarusfpc2.6.2binx86_64-win64fpc.cfg Hint: End of reading config file C:lazarusfpc2.6.2binx86_64-win64fpc.cfg Free Pascal Compiler version 2.6.2 [2014/02/28] for x86_64 Copyright (c) 1993-2012 by Florian Klaempfl and others Target OS: Win64 for x64 Compiling test.pp Linking test.exe test.pp(8,1) Error: Undefined symbol: TEST_INT_TEST_PROC test.pp(8,1) Fatal: There were 1 errors compiling module, stopping What is needed in the test_impl.pas file so the linker can find the implemented function? Do I need to create a library and export the function perhaps? Basically, you have two options: 1) Provide the import declaration with the mangled name (i.e. the name really stored within the .o file). This may possibly change between different FPC versions, but if you intend to link to an existing object file, you would already know which one was used. In your case, this would mean changing line 7 in test_int.pas to: procedure test_proc; external name 'TEST_IMPL_TEST_PROC'; (the mangled name may be found either by using -a when compiling test_impl.pas and looking into the generated test_impl.s or e.g. by using 'objdump -t test_impl.o'. 2) Specify your own name during rather than relying on some mangling rules - change line 7 in test_impl.pas to: procedure test_proc; export alias: 'My_Test_Proc'; and then line 7 in test_int.pas needs to change accordingly to: procedure test_proc; external name 'My_Test_Proc'; Tomas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [1]

Links:
------
[1] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email