Search code examples
c++linuxqtubuntusymlink

Using a relative path in executable with symbolic link


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


Solution

  • 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
    $