Recent

Author Topic: Error: linker: Undefined symbols for architecture x86_64:  (Read 2484 times)

daniel_sap

  • New Member
  • *
  • Posts: 40
Error: linker: Undefined symbols for architecture x86_64:
« on: March 20, 2021, 01:26:02 am »
Hi,

I created a simple library, with just one function.
After compiling it a libsimple.dylib file was generated.
I created a test lib project where I import the function.
Also, copied the dylan file in the project's folder, as well to x86_64-darwin

When compiling the project I received this error
Code: Text  [Select][+][-]
  1. Compile Project, Target: Libtest: Exit code 1, Errors: 4, Hints: 3
  2. Hint: Start of reading config file /Users/julian/FpcUpDeluxe/fpcupdeluxe/fpc/bin/x86_64-darwin/fpc.cfg
  3. Hint: End of reading config file /Users/julian/FpcUpDeluxe/fpcupdeluxe/fpc/bin/x86_64-darwin/fpc.cfg
  4. Verbose: Free Pascal Compiler version 3.3.1-r48994 [2021/03/17] for x86_64
  5. Verbose: Copyright (c) 1993-2021 by Florian Klaempfl and others
  6. Verbose: Target OS: Darwin for x86_64
  7. Verbose: Compiling Libtest.lpr
  8. Verbose: Compiling mainform.pas
  9. Verbose: Compiling simple.pas
  10. Verbose: Assembling simple
  11. mainform.pas(19,28) Verbose: Parameter "Sender" not used
  12. mainform.pas(34,3) Hint: Unit "simple" not used in MainForm
  13. Verbose: Assembling mainform
  14. Libtest.lpr(13,20) Verbose: Unit "simple" not used in Libtest
  15. Verbose: Assembling libtest
  16. Verbose: Compiling resource /Users/julian/Project/ProjectL/Applications/Research/Libtest/lib/x86_64-darwin/Libtest.or
  17. Verbose: Linking /Users/julian/Project/ProjectL/Applications/Research/Libtest/Libtest
  18. Error: linker: Undefined symbols for architecture x86_64:
  19. Error: linker:   "_sum2int", referenced from:
  20. Debug:   "_sum2int", referenced from:
  21. Debug:       _SIMPLE_$$_SUM2INT$LONGINT$LONGINT$$LONGINT in simple.o
  22. Error: ld: symbol(s) not found for architecture x86_64
  23. An error occurred while linking
  24. Error: Error while linking
  25. Verbose: There were 1 errors compiling module, stopping
  26. Verbose: Compilation aborted
  27. Verbose: /Users/julian/FpcUpDeluxe/fpcupdeluxe/fpc/bin/x86_64-darwin/ppcx64 returned an error exitcode
  28.  

the import code looks like this

Code: Pascal  [Select][+][-]
  1. unit simple;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. const
  11.   lib_path = '/Users/julian/Project/ProjectL/Libraries/simplelib/';
  12.   simple_lib = 'simple.dylib';
  13.   {$linklib simple.dylib}
  14.  
  15. function sum2int(val1, val2: Integer): Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  16. {$EXTERNALSYM sum2int}
  17.  
  18. implementation
  19.  
  20. function sum2int(val1, val2: Integer): Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  21.     external simple_lib name 'sum2int';
  22.  
  23. end.
  24.  
In the code I tried also with
simple_lib = 'simple';
simple_lib = 'simple.dylib';
simple_lib = 'libsimple.dylib';

I searched the web and the forum but the articles I read didn't help me much.
May be you can give some more information about the error.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Error: linker: Undefined symbols for architecture x86_64:
« Reply #1 on: March 20, 2021, 01:36:27 am »
Compare what you've done with my Wiki example in the macOS Dynamic Libraries article.

daniel_sap

  • New Member
  • *
  • Posts: 40
Re: Error: linker: Undefined symbols for architecture x86_64:
« Reply #2 on: March 20, 2021, 10:32:57 am »
Thank you @trev for helping again and for pointing me to the wiki example and that you wrote this example.
I'll check the differences.

daniel_sap

  • New Member
  • *
  • Posts: 40
Re: Error: linker: Undefined symbols for architecture x86_64:
« Reply #3 on: March 21, 2021, 10:15:56 pm »
Issue solved, it is completely different syntax compared to Delphi.

After reading the Wiki I choose dynamic linking of the library (not the static).
I edited the code as the second example for dynamic linking.

Code: Pascal  [Select][+][-]
  1. unit simple;
  2. {$linklib libsimple}
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. function sum2ints(val1, val2: Integer): Integer; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF}; external;
  8.  
  9. implementation
  10.  
  11. end.
  12.  

I like how simple is the import of library functions. It is simple like that in Java. In Delphi it looks unnecessary complicated.
« Last Edit: March 21, 2021, 10:37:47 pm by daniel_sap »

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 10.2 user
Re: Error: linker: Undefined symbols for architecture x86_64:
« Reply #4 on: March 21, 2021, 11:09:35 pm »
Good to hear you solved it. It's always easy when you know how  :D The Wiki is a great resource that is often overlooked.

 

TinyPortal © 2005-2018