您的位置:首页 > 运维架构 > Linux

linux 程序内部 查看可执行程序路径

2010-01-05 11:02 246 查看
linux shell pwd 显示当前路径

假若有test.cpp

g++ test.cpp -o test

./test

想在test中找到当前执行程序所在的路径

可以再test.cpp中使用readlink函数

具体见如下实例:

#include<iostream>

#include<unistd.h>

#include<dirent.h>

#include<string.h>

#include<string>

using namespace std;

int main()

{

char buff[1024];

memset(buff,0,sizeof(buff));

int n = readlink("/proc/self/exe",buff,1023);

if(n<0)

{

cout<<"Get Path failed"<<endl;

return -1;

}

string path = buff;

int nLen = path.rfind('/');

path.erase(nLen,path.length() - nLen);

cout<<"zui zhong lu jing :"<<path<<endl;

return 0;

}

谨记是/proc/self/exe 在此某人载过跟头
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: