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

来源:互联网 发布:新开淘宝店 编辑:程序博客网 时间:2024/06/03 11:16

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 在此某人载过跟头