Linux _NSGetExecutablePath 的替代方案?

Linux alternative to _NSGetExecutablePath?

是否可以在 Ubuntu Linux 上回避 _NSGetExecutablePath 以代替非 Apple 特定方法?

我正在尝试在 Ubuntu 上编译以下代码:https://github.com/Bohdan-Khomtchouk/HeatmapGenerator/blob/master/HeatmapGenerator2_Macintosh_OSX.cxx

根据我之前提出的这个问题:,我决定注释掉第 52 行,并想知道是否有一种通用的跨平台(非 Apple 特定)方式可以重写第 567 行的代码块(_NSGetExecutablePath 块)以非 Apple 特定的方式。

Alen Stojanov 对 Programmatically retrieving the absolute path of an OS X command-line app and also How do you determine the full path of the currently running executable in go? 的回答给了我一些关于从哪里开始的想法,但我想在开始做这件事之前确保我在正确的轨道上。

有没有办法修改_NSGetExecutablePath以兼容UbuntuLinux?

目前,我遇到以下编译器错误:

HeatmapGenerator_Macintosh_OSX.cxx:568:13: error: use of undeclared identifier
      '_NSGetExecutablePath'
        if (_NSGetExecutablePath(path, &size) == 0)

如何以跨 POSIX 个系统可移植的方式进行操作的基本想法:

#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

static char *path;
const char *appPath(void)
{
    return path;
}

static void cleanup()
{
    free(path);
}

int main(int argc, char **argv)
{
    path = realpath(argv[0], 0);
    if (!path)
    {
        perror("realpath");
        return 1;
    }

    atexit(&cleanup);

    printf("App path: %s\n", appPath());
    return 0;
}

您可以为其定义一个自己的模块,只需将其传递给 argv[0] 并从 header.

导出 appPath() 函数

编辑: 用访问器方法替换导出变量