5

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 ./executable_name.

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?

SternK
  • 11,649
  • 22
  • 32
  • 46
philus
  • 51
  • 1
  • 3
  • 1
    I'm guessing your app does not take `cwd()` into account and forgets to `chdir()` to its intended working directory. – Jesper Juhl Apr 16 '18 at 14:43
  • that's true. can you give some further explication please? like...how can I do that? thanks – philus Apr 16 '18 at 14:54
  • 1
    Start by reading the `man` pages of the two functions I mentioned. Also read this: https://stackoverflow.com/questions/933850/how-do-i-find-the-location-of-the-executable-in-c – Jesper Juhl Apr 16 '18 at 15:05
  • Your app isn't designed for GUI interaction yet. Hard to answer because it heavily depends on the framework you want to use. Anyway there is a lot of such question, look https://unix.stackexchange.com/questions/32797/dragndrop-execute-functionality-in-kde for example – Jean-Baptiste Yunès Apr 16 '18 at 15:39
  • you can have it print its current working directory upon startup, and if needed be, you can use some function to determine the location of itself. But how do you manage to run by double clicking an executable in linux? – Mefitico May 07 '20 at 22:23

3 Answers3

2

On Linux, you can add the -no-pie flag to your g++ compiler.

g++ main.cpp -o main -no-pie

difference between compiling with -no-pie and without it

This is more useful for graphical applications, that do not show anything on the terminal. If that's your case, then go ahead and do it.

This will make an executable that you can double click to execute.

ENIAC
  • 813
  • 1
  • 8
  • 19
0

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.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
0

"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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298