如何从里面找到C ++ Linux程序的完整路径? [英] How to find the full path of the C++ Linux program from within?

查看:117
本文介绍了如何从里面找到C ++ Linux程序的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个要求,我需要从里面找到C ++程序的完整路径。对于Windows,我有以下解决方案。 argv [0]可能包含或不包含完整路径。但我需要确定。

I have this requirement where I need to find the full path for the C++ program from within. For Windows, I have the following solution. The argv[0] may or may not contain the full path. But I need to be certain.

TCHAR drive[_MAX_DRIVE], dir[_MAX_DIR], base[_MAX_FNAME], ext[_MAX_EXT];
TCHAR fullPath[255+1];
_splitpath(argv[0],drive,dir,base,ext);
SearchPath(NULL,base,ext,255,fullPath,NULL);

上述代码的Linux(gcc)

What is the Linux (gcc) equivalent for the above code? Would love to see a portable code.

推荐答案

在Linux(Posix?)上有一个符号链接

On Linux (Posix?) you have a symbolic link /proc/self/exe which links to the full path of the executable.

在Windows上,使用 /msdn.microsoft.com/en-us/library/ms683197%28v=vs.85%29.aspx> GetModuleFileName

On Windows, use GetModuleFileName.

不要依靠 argv [0] ,这不能保证是有用的。

Never rely on argv[0], which is not guaranteed to be anything useful.

请注意,路径和文件系统不是语言的一部分,因此必须是平台相关功能。

Note that paths and file systems are not part of the language and thus necessarily a platform-dependent feature.

这篇关于如何从里面找到C ++ Linux程序的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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