Command Line Tool Executable from Finder?

Discussion in 'Mac Programming' started by Duke Leto, Apr 21, 2011.

  1. Duke Leto macrumors regular

    Mar 17, 2008
    I made a project in C that was a "command line tool" in XCode. It uses GLUT/OpenGL, so it does have a user interface. It works fine, and I can run it from Terminal, but I was wondering if there was any way to allow it to run from Finder.

    I did create a new target (a Cocoa application), and I included the linked frameworks and removed the prefix header and it compiles and runs. However, it won't work from Finder. I think that the problem is that my program relies on a file named "Input" that it assumes is in its directory. I tried using
    FILE* data = fopen("../../Input", "r");
    instead of
    FILE* data = fopen("Input", "r");
    but that didn't work. (I also tried more levels of up and down, that doesn't seem to work either.)

    I hope that that ramble was half-intelligible!

    Thanks in advance for any help.
  2. chown33 macrumors 604

    Aug 9, 2009
    In both cases of fopen(), you are assuming a particular value for current working directory, and from which your relative pathnames are assumed to be sensible.

    I suggest checking your assumption of working directory, perhaps by NSLog()'ing the actual value, such as returned by the C function getcwd(). You should read the man page for getcwd() to see what its parameters are.
  3. Duke Leto thread starter macrumors regular

    Mar 17, 2008
    I looked into getcwd, and I was able to get the directory of the executable. I wrote some code to modify the path to go up a couple of folders, and I verified that it did the correct modifications. Furthemore, I found that the program worked just fine when I opened the package contents of the .app file and ran the executable inside the MacOS folder from Terminal.
    However, the .app will not run from Finder.
  4. chown33 macrumors 604

    Aug 9, 2009
    Running the executable using Terminal, after cd'ing into the .app Contents/Resources is not the same thing as running it from Finder.

    You should look at the working directory when run from Finder, using the debugger if you have to.

    You're making a faulty generalization based on what happens in Terminal. In particular, I can almost guarantee that launching from Finder DOES NOT set the working directory to the location of the executable.

Share This Page