Search code examples
c++macoscommand-linexcode4.1

MacOSX C++ working directory with command line applications


I've been trying to figure out how to set the working directory to the same directory as my program. As I am unfamiliar with Objective-C and Cocoa, I wrote this as a basic command line application, meaning standard in/out. For some reason, the working directory is set to the $HOME directory as opposed to the same directory as the file itself. I know using

system("PWD");

would echo the path, but in an unusable (at least to me) format.

Edit: Just for a little clarification, I need to be able to use relative file paths for file streams on more than just my machine/user.


Solution

  • You can set the directory in which your tool will start when run from Xcode in the scheme editor. In the toolbar, click on the name of your project (to the left of the >), then click "Edit Scheme…", click on "Run" in the list on the left side, then the "Options" tab on the right, and you'll see an option to use a custom working directory.

    Note that you aren't guaranteed to be on a case-insensitive file system, so you should use a lowercase pwd or your system(3) call will fail. iOS never uses a case-insensitive filesystem, and OS X may be installed on either a case-insensitive or case-sensitive file system. getcwd(3) is more efficient/appropriate in any case.

    Also, you should upgrade to a current version of Xcode if you can. The early 4.x versions were very rough around the edges (some would say 4.4/4.5 still are, but they're at least more stable).