Recent

Author Topic: Library works on Window but not in Linux  (Read 2184 times)

cpalx

  • Hero Member
  • *****
  • Posts: 642
Library works on Window but not in Linux
« on: June 13, 2020, 11:56:10 pm »
Hello,
i made a library (.dll in Windows; .so for Linux)
in wndows works perfectly, but not in Linux, the error:

error while loading shared libraries: libneo_base64.so: cannot open shared object file: No such file or directory

The File is next to the executable, i copied in /usr/bin too.

the original file is neo_base64.so, but looking the error o created the libry libneo_base64.so

any idea?

Code: Pascal  [Select][+][-]
  1. {$ifdef windows}
  2.           NEO_Base64DLLName = 'neo_base64.dll';
  3.   {$endif}
  4.   {$ifdef unix}
  5.           NEO_Base64DLLName = 'neo_base64.so';
  6.   {$endif}
  7.   function FileToBase64(const AFile: Pchar): Pchar; external NEO_Base64DLLName;
  8.   function Base64ToFile(const Base64, AFile: Pchar): boolean; external NEO_Base64DLLName;  

winni

  • Hero Member
  • *****
  • Posts: 1616
Re: Library works on Window but not in Linux
« Reply #1 on: June 14, 2020, 12:20:06 am »
Hi!

Did you inform your linux system about the new library?

Code: Bash  [Select][+][-]
  1. sudo ldconfig

Winni

PascalDragon

  • Hero Member
  • *****
  • Posts: 1970
  • Compiler Developer
Re: Library works on Window but not in Linux
« Reply #2 on: June 14, 2020, 11:05:08 am »
The File is next to the executable, i copied in /usr/bin too.

Linux by default does not look in the bin directories for libraries. Instead you need to copy it to /usr/lib or similar (might also be /usr/lib64 depending on your distribution).

Leledumbo

  • Hero Member
  • *****
  • Posts: 8244
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Library works on Window but not in Linux
« Reply #3 on: June 21, 2020, 08:35:03 am »
The File is next to the executable, i copied in /usr/bin too.

Linux by default does not look in the bin directories for libraries. Instead you need to copy it to /usr/lib or similar (might also be /usr/lib64 depending on your distribution).
To be precise, it reads /etc/ld.so.conf, in which inside it may read another files. In KDE Neon, it reads everything in /etc/ld.so.conf.d, all of which combined forms several paths. No /usr/bin, though.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1035
    • Burdjia
Re: Library works on Window but not in Linux
« Reply #4 on: July 02, 2020, 11:41:05 am »
There are two ways:

The standard way is to put the ".so" files in one of the "lib" directories (depending the purpose and the distribution) then run the "ld" tool as root so the system updates the library database.

The other way is to change the library path before run the application telling where it should search for the library.  You can use a bash script like the next one:
Code: Bash  [Select][+][-]
  1. #!/bin/bash
  2. export OLD_PATH=$LD_LIBRARY_PATH
  3. export LD_LIBRARY_PATH=<where_the_so_file_is>:$LD_LIBRARY_PATH
  4. ./<your_app>
  5. export LD_LIBRARY_PATH=$OLD_PATH
  6.  
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Warfley

  • Sr. Member
  • ****
  • Posts: 287
Re: Library works on Window but not in Linux
« Reply #5 on: July 02, 2020, 02:22:52 pm »
The other way is to change the library path before run the application telling where it should search for the library.  You can use a bash script like the next one:
You don't even need to reset the LD_LIBRARY_PATH afterwards because it is shell local meaning this change will only be in the bash scripts own process. Also, if you only need to set this for a single command you can just tell bash to execute that command with the new env variable, so this can be shortend to
Code: Bash  [Select][+][-]
  1. #!/bin/bash
  2. LD_LIBRARY_PATH=<where_the_so_file_is>:$LD_LIBRARY_PATH ./<your app> $@

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1035
    • Burdjia
Re: Library works on Window but not in Linux
« Reply #6 on: July 03, 2020, 11:18:48 am »
Thanks for the tip.  :)
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

devEric69

  • Sr. Member
  • ****
  • Posts: 319
Re: Library works on Window but not in Linux
« Reply #7 on: July 03, 2020, 12:46:33 pm »
For info., there is a graph that summarizes the most common methods of loading a library under Linux, here: https://wiki.freepascal.org/Lazarus/FPC_Libraries#Simplified_overview_of_the_system.2C_when_loading_a_shared_library_under_Linux.
use: Linux 64 bits (Ubuntu 18.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

 

TinyPortal © 2005-2018