【发布时间】:2014-05-05 17:00:39
【问题描述】:
我正在将一个 Linux C++03 应用程序移植到 Darwin OS X,并且有一些代码可以读取 /proc/self/exe 处的符号链接以确定运行的可执行文件所在的目录。
如何计算当前在 Macintosh Darwin OS X Mavericks 上运行的 C++ 可执行文件的目录?
这是我现有的适用于 Linux 的代码:
bool
resolveBinaryLocation(string &binaryDirname)
{
// Read the symbolic link '/proc/self/exe'.
const char *linkName = "/proc/self/exe";
const size_t bufSize = PATH_MAX + 1;
char dirNameBuffer[bufSize];
const int ret = int(readlink(linkName, dirNameBuffer, bufSize - 1));
if (ret == -1) {
// Permission denied (We must be inetd with this app run as other than root).
return false;
}
dirNameBuffer[ret] = 0; // Terminate the string with a NULL character.
binaryDirname = dirNameBuffer;
// Erase the name of the executable:
string::size_type last = binaryDirname.size() - 1;
string::size_type idx = binaryDirname.rfind(DSI_PATH_CHAR, last);
// Add one to keep the trailing directory separator.
binaryDirname.erase(idx + 1);
return true;
}
【问题讨论】:
-
您是在设置一个带有捆绑包的应用程序还是一个 CLI exe?
-
你的问题几乎和stackoverflow.com/questions/1023306/…重复了
-
@RhythmicFistman 这是一个 CLI exe。
标签: c++ osx-mavericks porting c++03 darwin