C、Linux、getcwd/chdir():获取二进制路径
我想打开与二进制文件位置相关的多个文件(log4cxx 配置、其他日志等)。
不幸的是, getwd() 和 getcwd() 都给了我一个目录,我尝试在已知路径上运行二进制文件,而不是给我二进制文件所在的路径位于(以及数据所在的位置)。
如何获取应用程序的路径以通过 chdir() 来使用它?除了 argv[0]
之外还有什么方法并且不尝试解析 /proc/$PID/ (这不够便携)?
I want to open a number of files (log4cxx configs, other logs etc) relative to binary's location.
Unfortunately, both getwd()
and getcwd()
are giving me the directory, from which I try to run binary at known path, instead of giving me the path where the binary is located (and where the data is).
How to get the app's path to use it with chdir()
? Any methods besides argv[0]
and without trying to parse /proc/$PID/ (that's not portable enough)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遍历 PATH 并找到与 argv[0] 同名的可执行文件?
但是,最好为用户提供一种配置数据位置的方法。环境变量或配置文件或 CL 参数或某些东西。处理那些试图提供帮助但实际上很愚蠢的程序是非常令人沮丧的。
Walk the PATH and find an executable of the same name as argv[0]?
However, it would probably be better to provide the user a way to configure where the data is. An env var or config file or CL parameter or something. It's very frustrating dealing with programs that try to be helpful but are actually just stupid.
这正是 autoconf 的生活如果您希望除编写软件的程序员之外的任何人使用它,那么支持这些标准目录几乎是强制性的。正确设置后,要从主目录中进行调试,您只需将不同的
--prefix=
值传递给configure
即可。This is exactly the kind of thing autoconf lives for, and supporting those standard directories is pretty much mandatory if you ever want anyone other than the programmers who wrote your software to use it. Once set up properly, to debug out of your home directory all you have to do is pass a different
--prefix=
value toconfigure
.