-2

I'm trying to get the current executable's file path without the executable name at the end.

I'm doing:

uint32_t size = sizeof(path);

if (_NSGetExecutablePath(path, &size) == 0) {


}

else {

    printf("buffer too small; need size %u\n", size);
}



char* program_name = dirname(path); // To remove executable name from the path

It works the path output is: /Users/Me/Desktop/TNT/build/Debug/

But when i was going to add some gui by using SDL library, the output path was wrong:

/Users/Me/Desktop/TNT/build/Debug/TNT.app/

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user1104856
  • 147
  • 3
  • 14

1 Answers1

1

It's not wrong, it's right.

From http://en.wikipedia.org/wiki/Application_bundle#Mac_OS_X_application_bundles:

Application bundles are directory hierarchies, with the top-level directory having a name that ends with a .app extension.

TNT.app/ is the working directory (and the top-level directory of the application bundle) on Mac OSX. You can confirm this if you enter a command shell and cd to the directory.

gravitron
  • 3,514
  • 1
  • 20
  • 18
  • But if the path ends with TNT.app/ then it can't find my file... how to fix it? – user1104856 Dec 21 '11 at 15:12
  • I think you want to put your file in the TNT.app directory, generally on Mac that's how applications are bundled with everything under that directory. If you can't do that you could append "..\" to go one directory backwards. – gravitron Dec 21 '11 at 15:18
  • Thanks sir, but how can you add "..\" to go one directory back with : printf("executable path is: \"%s\"\n", program_name); – user1104856 Dec 21 '11 at 15:21
  • if you know the file is one directory back to reference it you could use open("../filename","r") or similar. – gravitron Dec 21 '11 at 15:23
  • When i made a consol program with same code it works, but why it dosent with gui? – user1104856 Dec 21 '11 at 15:28
  • console app was not built as a .app package, it was probably just a binary file. – gravitron Dec 21 '11 at 15:50