How to find the residing directory of a OS X application package - programmatically

前端 未结 3 1293
予麋鹿
予麋鹿 2021-01-15 16:03

I have a OS X application as a .app package which can reside in any arbitrary location in the filesystem. Is there a way to find the current path of the package programmatic

3条回答
  •  不要未来只要你来
    2021-01-15 17:00

    The bundle directory might be a problem with command line tools. So i use the following function which uses the mach-0 linker function to resolve shared libraries.

     static char* find_executable_filepath () noexcept 
     {
         char path[1];
         uint32_t size = 1;
         if (_NSGetExecutablePath(path, &size) == 0) return nullptr;
         char* res = (char*)malloc(size+1);
         if (_NSGetExecutablePath(res, &size) != 0) return nullptr;
         char* r = realpath(res, nullptr);
         free(res);
         return r; 
     }
    

提交回复
热议问题