简体   繁体   English

在Linux上获取程序的目录

[英]Getting the program's directory on Linux

I'm wondering how I can get the directory of my program on Linux. 我想知道如何在Linux上获取程序的目录。 For instance, if I have my program located under /home/myproject/ and I get the directory, it should be /home/myproject/ regardless of what directory I'm calling the program from. 例如,如果我的程序位于/home/myproject/并且得到该目录,则无论我从哪个目录调用该程序,它都应为/home/myproject/ I need this functionality because I need to be able to access a configuration file located under the same folder as my program, regardless of where the program's folder is located. 我需要此功能,因为无论程序文件夹位于何处,我都需要能够访问与程序位于同一文件夹下的配置文件。

I've tried using getcwd() , but here's what it does: 我尝试使用getcwd() ,但是它的作用是:

If I'm currently in the same folder as the program is, it will work. 如果我当前与程序位于同一文件夹中,则它将起作用。 However, if I was in /root and tried executing the program which is located under /home/myproject , it would give me /root . 但是,如果我在/root并尝试执行/home/myproject下的程序,它将给我/root

If I just do something like... 如果我只是做...

std::ifstream is("anotherfile");

It will work as long as I'm in the same directory, but it does the same as above when I'm not. 只要我在同一目录中,它就可以工作,但是当我不在同一目录中时,它的作用与上面相同。

On Linux, you could use /proc/ . 在Linux上,您可以使用/proc/ Read carefully proc(5) . 仔细阅读proc(5)

I'm suggesting reading the symlink in /proc/self/exe using readlink(2) . 我建议使用readlink(2)/proc/self/exe读取符号链接。 It gives your executable. 它提供了您的可执行文件。 You might use dirname(3) on it to get its directory. 您可以在其上使用dirname(3)来获取其目录。 Be also aware of realpath(3) which might be useful (actually not, since, as commented by Daniel Schepler, /proc/self/exe is a canonical path....). 还请注意realpath(3)可能有用(实际上没有用,因为/proc/self/exe Daniel /proc/self/exe Sepler评论说, /proc/self/exe是规范路径。。。)。

This is Linux specific, and won't work in rare pathological cases (your executable being removed or renamed during execution). 这是特定于Linux的,在罕见的病理情况下(在执行过程中将可执行文件删除或重命名)不起作用。 See this . 看到这个

Remember that Linux don't have folders (they are just a GUI artefact) but directories . 请记住,Linux没有文件夹 (它们只是GUI构件),而没有目录 See also opendir(3) , readdir(3) , closedir(3) , stat(2) , nftw(3) , etc.... 另请参见opendir(3)readdir(3)closedir(3)stat(2)nftw(3)等...

At last, the Unix tradition is to keep user-specific configuration files under $HOME (often with hidden dotfile, eg $HOME/.inputrc ) and system-wide configuration files under /etc/ . 最后,Unix的传统是将用户特定的配置文件保留在$HOME (通常带有隐藏的点文件,例如$HOME/.inputrc ),并将系统范围的配置文件保留在/etc/ You can get $HOME with getenv(3) as getenv("HOME") . 您可以使用getenv(3)作为getenv("HOME")获得$HOME See environ(7) . 参见environ(7) In pathological cases such a getenv could fail. 在病理情况下,这种getenv可能会失败。

BTW, you could even adopt a convention of testing with getenv if some particular environment variable is set (eg MYPROGCONFIG ) and if it is set, use that as your configuration file. 顺便说一句,如果设置了某些特定的环境变量(例如MYPROGCONFIG ),并且甚至将其用作配置文件,您甚至可以采用getenv的测试约定。 Don't forget to document such conventions. 不要忘记记录这些约定。

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

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