【问题标题】:Linux alternative to _NSGetExecutablePath?Linux 替代 _NSGetExecutablePath?
【发布时间】:2015-09-28 03:31:00
【问题描述】:

是否可以在 Ubuntu Linux 上绕过 _NSGetExecutablePath 来代替非 Apple 特定的方法?

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

根据我之前提出的这个问题:fatal error: mach-o/dyld.h: No such file or directory,我决定注释掉第 52 行,并想知道是否有一种通用的跨平台(非 Apple 特定)方式可以重写代码块567(_NSGetExecutablePath 块)以非 Apple 特定的方式。

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

有没有办法修改_NSGetExecutablePath 以兼容Ubuntu Linux?

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

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

【问题讨论】:

    标签: linux macos ubuntu compiler-errors


    【解决方案1】:

    如何以一种可以跨 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] 传递给它并从标头中导出appPath() 函数。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2022-07-12
      • 2016-07-04
      • 2017-09-17
      • 2018-08-07
      相关资源
      最近更新 更多