简体   繁体   English

在带有符号链接的可执行文件中使用相对路径

[英]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.我正在尝试弄清楚如何使用 ubuntu 中的链接使用我的应用程序。我已经编译了代码,它包含某些文件的相对路径。 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谢谢

Is it realpath you're after? 您追求的是realpath吗? Something like this (source for test in below example): 这样的东西(下面示例中的test源):

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

In linux:在 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.如果在可执行文件之外,这是一个快速简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM