Linux上运行程序目录? [英] Directory of running program on Linux?

查看:171
本文介绍了Linux上运行程序目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我一直在写一个程序(一种电子书查看类型的东西),并从可执行文件所在的文件夹中的文件夹加载文本文件。这给我一个问题,因为如果我运行程序从另一个目录与命令./folder/folder/program例如,我的程序将找不到文本,因为工作目录不正确。我不能有一个绝对的目录,因为我想程序是便携式的。有任何方法来获取可执行文件运行的精确目录,即使它已从不同的目录运行。我听说可以结合argc [0]和getcwd(),但argc被截断,当目录中有一个空格,(我想?)所以我想避免,如果可能的话。

Hey, I've been writing a program (a sort of e-Book viewing type thing) and it loads text files from a folder within the folder of which the executable is located. This gives me a bit of a problem since if I run the program from another directory with the command "./folder/folder/program" for example, my program will not find the text, because the working directory isn't correct. I cannot have an absolute directory because I would like the program to be portable. Is there any way to get the precise directory that the executable is running from even if it has been run from a different directory. I've heard could combine argc[0] and getcwd() but argc is truncated when there is a space in the directory, (I think?) so I would like to avoid that if possible.

我在Linux上使用g ++,Thanx提前

I'm on Linux using g++, Thanx in advance

推荐答案

编辑 - 不要使用getcwd (),它只是用户不在可执行文件的位置。

EDIT - don't use getcwd(), it's just where the user is not where the executable is.

请参阅这里有详细信息

在linux / proc /< pid> / exe或/ proc / self / exe应该是一个指向可执行文件的符号链接。像其他人一样,我认为更重要的问题是你为什么需要这个?这不是真正的UNIX形式使用可执行路径查找辅助文件。而是使用环境变量或默认位置,或遵循其他约定来找到辅助文件的位置(即〜/.& lt; myapp> rc)。

On linux /proc/<pid>/exe or /proc/self/exe should be a symbolic link to your executable. Like others, I think the more important question is "why do you need this?" It's not really UNIX form to use the executable path to find ancillary files. Instead you use an environment variable or a default location, or follow one of the other conventions for finding the location of ancillary files (ie, ~/.<myapp>rc).

这篇关于Linux上运行程序目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆