Author Topic: .o C file and FPC unit  (Read 3579 times)


  • Newbie
  • Posts: 4
.o C file and FPC unit
« on: October 27, 2016, 11:17:57 am »
Hi Guys,

I have some code I have written in C (header file called lazspi.h and c file called lazspi.c)  which I have compiled using gcc with the -lm flag.

I have successfully managed to include the .o file and use the functions in my pascal Application. (using The problem (or annoying more than anything) is that I have to always make some sort of change (like delete an empty line, or add an empty line) to the pascal unit which is using the .o file before the compiler will successfully recompile, otherwise i get "undefined reference to (method name)" error for each method being linked to in the .o file. It always seems to be for the last method using the external C methods.

- Run compile
- Successful compile
- Stop run
- Run compile
- Get Errors shown below
- Add a line or remove a line from the lazspi unit
- Run compile
- Successful compile

Errors in messages window:
Code: Text  [Select]
  1. /usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
  2. ../lib/arm-linux/lazspi.o: in Function 'SENDMOTORPOS':
  3. ../lazspi.pas:70: undefined reference to 'Select_ADC1'
  4. ../lazspi.pas:73: undefined reference to 'get_ADC'
  5. ../lazspi.pas:73: undefined reference to 'Select_ADC0'
  6. ../lazspi.pas:73: undefined reference to 'get_ADC'
  7. ../lazspi.pas:70: undefined reference to 'init_cs_pins'
  8. ../lazspi.pas:71: undefined reference to 'spi_init'
  9. ../lazspi.pas:71: undefined reference to 'SendMPos'  
  10. ProgramName.ipr(28)Error: Error while linking
  11. ProgramName.ipr(28)Fatal: There were 1 errors compiling module, stopping

Im using this unit in a large program so having to navigate to it and tap enter or delete before checking changes gets tiresome.

Im running Lazarus V1.2.4 (fpc V2.6.4) on an Olimex A20 (Allwinner A20 CPU) with Linux (Debian Jessie)

Can anyone point out something that may cause this?

I have included the lazspi.h header file and the pascal unit function declarations. I have left out the lazspi.c file

lazspi.h C file
Code: C  [Select]
  1. ifndef LAZSPI_H
  2. #define LAZSPI_H
  3. int spi_init();
  4. void init_cs_pins() ;
  5. int get_ADC(int fd) ;
  6. void Select_ADC0() ;
  7. void Select_ADC1() ;
  8. void SendMPos(int Pos) ;
  9. #endif

Lazspi FPC Unit:
Code: Pascal  [Select]
  1. unit lazspi ;
  3. {$link lazspi.o}
  4. {$linklib c}
  6. interface
  8. uses
  9. CTypes, math ;
  11. function spi_init : integer; cdecl;external;
  12. procedure init_cs_pins ;cdecl;external;
  13. procedure Select_ADC0 ;cdecl;external;
  14. procedure Select_ADC1 ;cdecl;external;
  15. function get_ADC(fd : integer) integer; cdecl;external;
  16. procedure sendMPos(pos : integer) ; cdecl;external;
  18. {
  19. my own declared methods here
  20. }
  22. implementation
  24. {
  25. my own method implementation here
  26. }
  29. ---------------------
  30. end.

Apologies for the long post. Any help would be greatly appreciated.  :)

Thanks in advanced.


  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8101
Re: .o C file and FPC unit
« Reply #1 on: October 27, 2016, 11:24:21 am »
I think that is normal. The pascal->C bond is outside the unit system that governs automatic recompilation.

You can /force/ a full build though (instead of the default "compile"  which is incremental), then you won't need the edit trick.

Or you need to go to a more "make" build system, but that doesn't play nice with the ide.


  • Newbie
  • Posts: 4
Re: .o C file and FPC unit
« Reply #2 on: October 27, 2016, 12:36:44 pm »
I understand. Is there possibly a way to force a recompile of the unit alone with a directive or the likes?

Otherwise i will need to investigate forcing a full build.

Thanks for your response and suggestions


  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8101
Re: .o C file and FPC unit
« Reply #3 on: October 27, 2016, 12:41:44 pm »
No, but maybe a workaround is possible, some prebuild action that removes the relevant units .ppu and .o or touches the source.

But I would only invest in that if you really do it all the time. For a few times a day, just do run->build


  • Newbie
  • Posts: 4
Re: .o C file and FPC unit
« Reply #4 on: October 27, 2016, 12:57:15 pm »
Alright, Perfect.

Thanks again!  :D