I have coded a C++ software that needs to read files from the folder where it is located and than writes other files in the same folder.
For the moment I navigate via shell to the folder where I have copied the executable and then I launch it with
I would like to execute it by double-click. If I do that the software tell me that it cannot find the input files, then it is like it isn't executed in the good folder.
In my opinion there are two ways: 1) continue to copy the executable in the working folder and tell it to look for the files in this folder. 2) create a bash script that calls the executable in a given folder, like applications, and copy this script in the working folder. then tell the script to execute the software like if it was in the working folder. 3) you may have better ideas
In any case I do not have any idea on how to do it.
Can someone please help me?
Thanks a lot!!c++linuxmacosfolderexecutable
Most desktop software should execute a program with the current working directory (CWD) set to the executable's location. Most also support desktop links or shortcuts to programs which allow you to set the CWD yourself. This is probably the easiest route, but it will depend on what desktop environment you're running.
Your next option would be to write a shell script / batch file that executes your program in its own directory:
#!/bin/sh cd /path/to/program ./myprogram
Then execute that from your desktop instead of executing the executable directly.
Finally, you can make the program itself CWD-agnostic. To do this, you need to add path resolution code everywhere you pass a path to a system call. First you need to know the location of the executable. There is no standards-compliant way to do this in C++, but there are platform-specific ways. Once you know that path, you can resolve all relative paths against that base path. There are several existing questions on StackOverflow about how to do that; here's a place to start.
"Execute by Double Click" is a pretty ambiguous operation, with respect to the process working directory (pwd). There's no strict specification if the (pwd) of an executable launched through a graphical file manager should be the directory currently selected in the file manager, or if it shall be the directory the file manager was started from.
The only clean way to solve your problem is to register your program as a file type handler operating on directories, so that it shows up in the context menu of directories (https://www.freedesktop.org/wiki/Specifications/mime-apps-spec/). On a side note it's probably a good idea to give your program the ability to take the target directory as a command line parameter and do the directory change internally.
Registering as a file type handler has the nice side effect, that you can keep a single copy of your program at a central location, without the need to copy around stuff.