c++ - Windows 与 Linux GCC argv[0] 值

标签 c++ windows linux path

<分区>

Possible Duplicate:
Get path of executable

我在 Windows 上使用 MinGW、gcc 4.4.3 进行编程。当我像这样使用主要功能时:

int main(int argc, char* argv[]){
    cout << "path is " << argv[0] << endl;
}

在 Windows 上,我得到这样的完整路径:“C:/dev/stuff/bin/Test”。然而,当我在 Linux 上运行相同的应用程序时,我得到了某种相对路径:“bin/Test”。它破坏了我的应用程序!关于如何确保两个系统上的路径都是绝对路径的任何想法?

最佳答案

不,没有。在 Linux 上的大多数 shell 下,argv[0] 包含用户键入 以运行二进制文件的确切内容。这允许二进制文件根据用户类型执行不同的操作。

例如,具有多个不同命令行命令的程序可能会安装一次二进制文件,然后硬链接(hard link)各种不同的命令到同一个二进制文件。例如,在我的系统上:

$ ls -l /usr/bin/git*
-rwxr-xr-x  109 root  wheel  2500640 16 May 18:44 /usr/bin/git
-rwxr-xr-x    2 root  wheel   121453 16 May 18:43 /usr/bin/git-cvsserver
-rwxr-xr-x  109 root  wheel  2500640 16 May 18:44 /usr/bin/git-receive-pack
-rwxr-xr-x    2 root  wheel  1021264 16 May 18:44 /usr/bin/git-shell
-rwxr-xr-x  109 root  wheel  2500640 16 May 18:44 /usr/bin/git-upload-archive
-rwxr-xr-x    2 root  wheel  1042560 16 May 18:44 /usr/bin/git-upload-pack
-rwxr-xr-x    1 root  wheel   323897 16 May 18:43 /usr/bin/gitk

请注意其中一些文件的大小完全相同。更多调查显示:

$ stat /usr/bin/git
234881026 459240 -rwxr-xr-x 109 root wheel 0 2500640 "Oct 29 08:51:50 2011" "May 16 18:44:05 2011" "Jul 26 20:28:29 2011" "May 16 18:44:05 2011" 4096 4888 0 /usr/bin/git
$ stat /usr/bin/git-receive-pack 
234881026 459240 -rwxr-xr-x 109 root wheel 0 2500640 "Oct 29 08:51:50 2011" "May 16 18:44:05 2011" "Jul 26 20:28:29 2011" "May 16 18:44:05 2011" 4096 4888 0 /usr/bin/git-receive-pack

inode 号 (459240) 相同,因此这是指向磁盘上同一文件的两个链接。运行时,二进制文件使用 argv[0] 的内容来确定要执行的函数。你可以在 code for Git's main() 中看到这个(某种程度上) .

关于c++ - Windows 与 Linux GCC argv[0] 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7941536/

相关文章:

c++ - 问题应用鼠标冲动

linux - 在 Unix 中,我可以在一个目录中运行 'make' 而不先 cd'ing 到那个目录吗?

linux - 使用 HTTP 存储 git 命令的凭据

c++ - 如何生成具有唯一值的 vector ?

c++ - 使用 g++ 的 TCP 服务器客户端

windows - 用于与 Windows 命名管道通信的程序

windows - AWS ec2 windows 登录错误说发生了身份验证错误。无法联系本地安全机构

windows - 如何在 Windows 下使用特定的 PATH 安装 tomcat?

ruby - nginx配置有什么问题?

C++函数,我可以为对象提供什么默认值?