How to get the folder of the executable?

Just starting out? Need help? Post your questions and find answers here.
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

How to get the folder of the executable?

Post by Martin Verlaan »

What is the correct way to get the program folder? I mean the folder of the compiled program.

I use GetCurrentDirectory(), which works fine under Linux Mint and Windows. But in Debian, GetCurrentDirectory() points to my source folder and not the compiled folder. Is this a bug or do I use a wrong command?
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: How to get the folder of the executable?

Post by Josh »

You mean this?

Code: Select all

GetPathPart (ProgramFilename())
sorry for my bad english
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to get the folder of the executable?

Post by Martin Verlaan »

Josh wrote:You mean this?

Code: Select all

GetPathPart (ProgramFilename())
No, I need a command that returns the folder (path) of the compiled program. And if a user move or copy the folder to a another folder, it should return the path of that new folder.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to get the folder of the executable?

Post by Marc56us »

As its name indicates, GetCurrentDirectory() gives the current directory, i.e. the one where you are or the one defined at the start of the parent program or the one (re)defined by SetCurrentDirectory() and not necessarily the one of the generated executable.

Under Linux, it is the one which is displayed if we type the command 'pwd' in shell

Program, path and file:

Code: Select all

Debug ProgramFilename()
Debug GetPathPart( ProgramFilename() )
Debug GetFilePart( ProgramFilename() )
GetPathPart() returns the full path of the program when it is launched, so if this program is moved, of course-on the path also changes and indicates the new path (and not the compilation path)
If that was the question?

:wink:
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to get the folder of the executable?

Post by Martin Verlaan »

I am sorry, I read Josh answer to quick and I didn't realized ProgramFileName() is a Purebasic function. It's the correct answer, so thanks to both of you for your quick help
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to get the folder of the executable?

Post by Martin Verlaan »

It appears that the problem is caused by the import command. I have no idea how to fix it. Whatever I try, the executable points to my source directory instead of the folder that includes the executable. Apparently it is not wise to use subfolders with the import command.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: How to get the folder of the executable?

Post by Little John »

Can you please post some executable code (preferably as short as possible), that demonstrates the issue? For investigating the problem, we first must be able to reproduce it.
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to get the folder of the executable?

Post by Martin Verlaan »

I tried again to import without using subfolders, but still the same problem. This is what I did:

In Linux, I keep my source code in this folder /var/www/data/purebasic/sourcecode/smartmix versie 1.24
In that folder I have this library file: libbass.so which I import with:

Code: Select all

Import "libbass.so"
; ... some code here ...
EndImport
If can succesfully run this code from the Purebasic Editor.

Then I created an executable and put it in a new folder. In that folder I also copied libbass.so
If I run the program from this folder in Linux Mint, it works fine.

But if I run the same compiled program in Debian (from VirtualBox), I get this error:
Error while loading shared libraries: /var/www/data/purebasic/sourcecode/smartmix versie 1.24/libbass.so: cannot open shared object file: No such file or directory
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to get the folder of the executable?

Post by ts-soft »

Use allways this:
Image

and the code from josh.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to get the folder of the executable?

Post by Martin Verlaan »

I enabled that option but it doesn't help. And import command only accept a literal string, so something like this is not possible:
import GetPathPart(ProgramFilename()) + "folder1\folder2\lib.so"
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: How to get the folder of the executable?

Post by ts-soft »

Code: Select all

ImportC "./folder1/folder2/lib.so"
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to get the folder of the executable?

Post by Martin Verlaan »

This also makes no difference. :(
ts-soft wrote:

Code: Select all

ImportC "./folder1/folder2/lib.so"
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get the folder of the executable?

Post by wilbert »

As far as I understand, Linux doesn't search for a .so file in the folder of the executable.
You would either have to place the .so file in one of the standard library folders or add the executable path manually to the places where should be looked for the .so file ( rpath ).
Another option is to use OpenLibrary() / GetFunction() together with PrototypeC instead of importing the functions.
Windows (x64)
Raspberry Pi OS (Arm64)
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to get the folder of the executable?

Post by Martin Verlaan »

wilbert wrote:As far as I understand, Linux doesn't search for a .so file in the folder of the executable.
You would either have to place the .so file in one of the standard library folders or add the executable path manually to the places where should be looked for the .so file ( rpath ).
Another option is to use OpenLibrary() / GetFunction() together with PrototypeC instead of importing the functions.
Ah! That explains the problem. Although I don't understand why it does work in Mint (maybe because it's more flexible). I'm going to try OpenLibrary(), thanks Wilbert!
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get the folder of the executable?

Post by wilbert »

What also might work if you want to use ImportC is to add this extra line to the code.

Code: Select all

ImportC "-Wl,-rpath,'$ORIGIN'" : EndImport
I'm not sure but it might make a difference.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply