简体   繁体   English

Linux替代_NSGetExecutablePath?

[英]Linux alternative to _NSGetExecutablePath?

Is it possible to side-step _NSGetExecutablePath on Ubuntu Linux in place of a non-Apple specific approach? 是否可以在Ubuntu Linux上使用_NSGetExecutablePath代替非Apple特定的方法?

I am trying to compile the following code on Ubuntu: https://github.com/Bohdan-Khomtchouk/HeatmapGenerator/blob/master/HeatmapGenerator2_Macintosh_OSX.cxx 我想在Ubuntu上编译以下代码: https//github.com/Bohdan-Khomtchouk/HeatmapGenerator/blob/master/HeatmapGenerator2_Macintosh_OSX.cxx

As per this prior question that I asked: fatal error: mach-o/dyld.h: No such file or directory , I decided to comment out line 52 and am wondering if there is a general cross-platform (non-Apple specific) way that I can rewrite the code block of line 567 (the _NSGetExecutablePath block) in a manner that is non-Apple specific. 根据我之前提出的问题: 致命错误:mach-o / dyld.h:没有这样的文件或目录 ,我决定评论第52行,我想知道是否有一般的跨平台(非Apple特定)我可以以非Apple特定的方式重写第567行的代码块( _NSGetExecutablePath块)。

Alen Stojanov's answer to 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? Alen Stojanov回答以编程方式检索OS X命令行应用程序的绝对路径,以及如何确定当前运行的可执行文件的完整路径? gave me some ideas on where to start but I want to make certain that I am on the right track here before I go about doing this. 给了我一些关于从哪里开始的想法,但我想在做这个之前确定我在正确的轨道上。

Is there a way to modify _NSGetExecutablePath to be compatible with Ubuntu Linux? 有没有办法修改_NSGetExecutablePath以与Ubuntu Linux兼容?

Currently, I am experiencing the following compiler error: 目前,我遇到以下编译器错误:

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

Basic idea how to do it in a way that should be portable across POSIX systems: 基本思路如何以跨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;
}

You can define an own module for it, just pass it argv[0] and export the appPath() function from a header. 您可以为它定义一个自己的模块,只需将其传递给argv[0]并从头部导出appPath()函数。

edit : replaced exported variable by accessor method edit :通过访问器方法替换导出的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM