How to get the folder of the executable?

Just starting out? Need help? Post your questions and find answers here.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to get the folder of the executable?

Post by Marc56us »

I don't know if this will work, but in the default Linux PB installation, there is a dot in Library Subsystem (= search in the executable folder)

Put your lib in your executable folder

In preferences
[X] Create temporary executable in the source directory
Library Subsystem: . (point)

:wink:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: How to get the folder of the executable?

Post by RASHAD »

Maybe the problem is between Debian and VirtualBox
Just a suggestion
Egypt my love
Martin Verlaan
Enthusiast
Enthusiast
Posts: 134
Joined: Sun Apr 01, 2018 11:26 am
Location: Netherlands
Contact:

Re: How to get the folder of the executable?

Post by Martin Verlaan »

No, it also happens in Debian directly installed on PC (a user who is using Debian drew my attention to this problem, that why I installed Debian in VirtualBox).

I tried all above suggestions, but none with success. I think the best solution is OpenLibrary(), but I tried that as well and it generates an memory error. I used this example https://www.un4seen.com/stuff/bass24-pb.zip as a test and replaced the .dll filenames with .so but maybe that's not the correct way for Linux.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get the folder of the executable?

Post by wilbert »

Martin Verlaan wrote:I think the best solution is OpenLibrary(), but I tried that as well and it generates an memory error. I used this example https://www.un4seen.com/stuff/bass24-pb.zip as a test and replaced the .dll filenames with .so but maybe that's not the correct way for Linux.
It's an old include file for 32 bit.
You will need to replace quite a bit of .l (Long) types into .i (Integer) to make it work on 64 bit.

Edit:
I might have been to fast with my conclusion.
Most things seem to work fine without converting .l to .i
Last edited by wilbert on Fri Aug 10, 2018 4:08 pm, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: How to get the folder of the executable?

Post by #NULL »

Maybe some distros search for shared libraries in the current directory per default, but usually they don't.
You can add '.' to LD_LIBRARY_PATH before running the binary. Or you properly install the library in your system including running ldconfig so its being found in the default locations.
For a test you may just open a terminal in the binary directory and try

Code: Select all

export LD_LIBRARY_PATH=. && ./binary
where binary is you executable name.
Martin Verlaan
Enthusiast
Enthusiast
Posts: 134
Joined: Sun Apr 01, 2018 11:26 am
Location: Netherlands
Contact:

Re: How to get the folder of the executable?

Post by Martin Verlaan »

Still there is one thing I don't understand. I am using an relative path with Import, so why does Import refers to my source folder and not to the folder in which it was compiled and saved. To me, this looks like a bug.
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: How to get the folder of the executable?

Post by #NULL »

You are right. If you move the binary and the *.so around then the binary will still use the lib from the original source folder. If you rename the original lib file then the copied binary won't run. I don't know if this is right or wrong though. 'Create executable in the source directory' doesn't change anything about that and is only for running from IDE.

mylib.pb

Code: Select all

; executable format 'Shared.so'
; create executable to 'mylib', creates mylib.so

ProcedureDLL.i f(p.i)
  ProcedureReturn p * 2
EndProcedure
main.pb

Code: Select all

; create executable 'main'

Import "mylib.so"
  f(p.i)
EndImport

MessageRequester("test", "f(123): " + Str(f(123)))
then copy main and mylib.so elsewhere. main will run, but not if you rename the mylib.so in the original folder.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to get the folder of the executable?

Post by Marc56us »

Linux Debian is configured by default as a normal Unix (i.e. secured against misuse)
So there is no path on the current directory. (verify: echo $PATH does not end with :.)
Add an . in the PATH variable and try again

:?:
Martin Verlaan
Enthusiast
Enthusiast
Posts: 134
Joined: Sun Apr 01, 2018 11:26 am
Location: Netherlands
Contact:

Re: How to get the folder of the executable?

Post by Martin Verlaan »

Okay, learned a lot today. I will use import "\usr\lib\smartmix\file.so" and I will include a folder "smartmix" with the executable. I will tell the users of my program that they must copy this folder to \usr\lib\ and then everything will be fine :mrgreen: Thanks everybody for your replies.

Oh and the reason it works here in Linux Mint, is because the source code folder exist there! In a clean installed Mint it will not work (just like Debian), so I have to follow above method for that distro as well. I am happy that I understand it now.
Post Reply