4

I'm trying to figure out how to use my application with a link in ubuntu. I've compiled the code and it contains relative paths to certain files. When I create a link to the executable in a different directory, I can't use these paths. Is there a way (in my code or in the creation of the link) to make it work with the relative paths?

Thanks

JLev
  • 705
  • 1
  • 9
  • 30
  • A link is no different from invoking your application using a full or relative path to it. The current working directory is not the same as your application's location. It is usual in Unix OS(s) to have files somewhere specific under the users directory or in a known full path see the Unix (like) file system documentation for details. Big read: http://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html – Richard Critten Jun 11 '17 at 15:25
  • when writing a shell script there is a similar problem when calling the shell script from a different directory. This problem is solved by changing to the scripts directory first, before running any commands. In bash, argument 0 is the name of the script. I'm not familiar with how this would be done since you are using a compiled program. Perhaps this will help https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within – yosefrow Jul 09 '17 at 05:10

2 Answers2

1

Is it realpath you're after? Something like this (source for test in below example):

#include <iostream>
#include <cstdlib>

int main(int argc, char *argv[])
{
        char *path = realpath(argv[0], NULL);
        std::cout << path << '\n';
        free(path);
        return 0;
}

Example execution:

$ ln -s tmp/test
$ ./test
/home/mlil/tmp/test
$
Mats
  • 101
  • 5
0

In linux:

ln -sr <source relative path> <destination relative path>

You can verify the symbolic link created in the desination by navigating to that directory and typing the command:

ls -l

The accepted answer is the one that should be used if it is an executable, which is what your question is about. If outside an executable, this is a quick an easy solution.

Pranav Kumar
  • 254
  • 2
  • 7