在Linux下获取当前运行程序可执行文件全路径,不使用real_path

http://hi.baidu.com/xchinux/blog/item/2f71f50303f2f7e409fa9343.html

在Linux下获取当前运行程序可执行文件全路径(不使用real_path()函数)

2009-04-05 17:03

linux下的real_path()函数在Mandriva 2009中出现了buffer overflow的错误,这可能是它的一个BUG,现在通过在启动脚本(.sh)中切换到可执行文件所在的目录的方式解决获取可执行文件路径的问题:

程序:

int main(int argc, char **argv)

{

.................

boost::filesystem::path appPath;

#ifdef _WIN32

appPath = boost::filesystem::complete(argv[0]);

#else

appPath = boost::filesystem::current_path() / "MyExeFileName";

#endif

..............

}

启动脚本:

#!/bin/sh

exedir=`dirname $0`

LD_LIBRARY_PATH=$exedir/../lib:/usr/lib:$LD_LIBRARY_PATH

export LD_LIBRARY_PATH

cd $exedir

./MyExeFileName $1