Linux alternative to _NSGetExecutablePath?
Is it possible to use a side step _NSGetExecutablePath
on Ubuntu Linux instead of the 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 in the previous question I asked: fatal error: mach-o / dyld.h: No such file or directory , I decided to comment out line 52 and I'm wondering if there is a general cross-platform (non-Apple) way that I can rewrite the line 567 code block (block _NSGetExecutablePath
) in a way that is independent of Apple.
Alain Stoyanov's answer to Programmatically retrieves the absolute path for an OS X command line application , and How do you determine the full path of the current executable in go? gave me some ideas on where to start, but I want to make sure I'm on the right track here before I do.
Is there a way to change _NSGetExecutablePath
for compatibility with Ubuntu Linux?
I am currently experiencing the following compiler error:
HeatmapGenerator_Macintosh_OSX.cxx:568:13: error: use of undeclared identifier
'_NSGetExecutablePath'
if (_NSGetExecutablePath(path, &size) == 0)
source to share
The basic idea of how to do this to be portable on POSIX systems is:
#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 your own module for it, just pass it in argv[0]
and export the function appPath()
from the header.
edit : replace exported variable with accessor method
source to share