How to get the folder of the executable?
-
- Enthusiast
- Posts: 133
- Joined: Sun Apr 01, 2018 11:26 am
How to get the folder of the executable?
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?
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?
Re: How to get the folder of the executable?
You mean this?
Code: Select all
GetPathPart (ProgramFilename())
sorry for my bad english
-
- Enthusiast
- Posts: 133
- Joined: Sun Apr 01, 2018 11:26 am
Re: How to get the folder of the executable?
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.Josh wrote:You mean this?
Code: Select all
GetPathPart (ProgramFilename())
Re: How to get the folder of the executable?
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:
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?
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() )
If that was the question?
-
- Enthusiast
- Posts: 133
- Joined: Sun Apr 01, 2018 11:26 am
Re: How to get the folder of the executable?
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
-
- Enthusiast
- Posts: 133
- Joined: Sun Apr 01, 2018 11:26 am
Re: How to get the folder of the executable?
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.
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: How to get the folder of the executable?
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.
-
- Enthusiast
- Posts: 133
- Joined: Sun Apr 01, 2018 11:26 am
Re: How to get the folder of the executable?
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:
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
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
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
Re: How to get the folder of the executable?
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.
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
-
- Enthusiast
- Posts: 133
- Joined: Sun Apr 01, 2018 11:26 am
Re: How to get the folder of the executable?
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"
import GetPathPart(ProgramFilename()) + "folder1\folder2\lib.so"
Re: How to get the folder of the executable?
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.
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
-
- Enthusiast
- Posts: 133
- Joined: Sun Apr 01, 2018 11:26 am
Re: How to get the folder of the executable?
This also makes no difference.
ts-soft wrote:Code: Select all
ImportC "./folder1/folder2/lib.so"
Re: How to get the folder of the executable?
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.
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)
Raspberry Pi OS (Arm64)
-
- Enthusiast
- Posts: 133
- Joined: Sun Apr 01, 2018 11:26 am
Re: How to get the folder of the executable?
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 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.
Re: How to get the folder of the executable?
What also might work if you want to use ImportC is to add this extra line to the code.
I'm not sure but it might make a difference.
Code: Select all
ImportC "-Wl,-rpath,'$ORIGIN'" : EndImport
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)