3

Is it possible to side-step _NSGetExecutablePath on Ubuntu Linux in place of a non-Apple specific approach?

I am trying to compile the following code on 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.

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? 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?

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)
Community
  • 1
  • 1
warship
  • 2,924
  • 6
  • 39
  • 65

1 Answers1

0

Basic idea how to do it in a way that should be portable across POSIX systems:

#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.

edit: replaced exported variable by accessor method