PDA

View Full Version : Problems running program in Terminal after compiling in Xcode.




Flint-Paper
May 17, 2011, 04:02 PM
Hi, newbie programmer here. I've given myself a crash course in C, and so that's the language I'm working in at the moment.

The problem I have is not so much with compiling, but with the resulting program. When I compile the program in Xcode, it does not work properly. It can't open a file: a call to fopen appears to return NULL.

When I compile the same files in the terminal, or in the Xcode debugging console, the program works fine. The call to fopen works and returns a non-NULL value. The issue only seems to be with running it in the terminal after it has been compiled in Xcode...

Any hints here would be well appreciated.



chown33
May 17, 2011, 04:16 PM
Post your code.


I suspect you're using a relative path for the file you want to open, and that the working directory of the program is set to the directory containing the executable in Xcode.

To see what the actual working directory is, use the getcwd() C function. Look up its man page using Xcode. Print the string using printf() and a suitable %s format.


You need to find the setting in Xcode that sets the working directory when you run the program in Xcode. The exact directions on where the setting is differs for Xcode 4 vs. Xcode 3.*.

Google search terms: xcode working directory

Example result:
http://forums.macrumors.com/showthread.php?t=286054

Flint-Paper
May 18, 2011, 04:47 AM
Thanks for the reply. Can't post my code right now because I'm on my phone while on a train.

This offers some clues but I'm still unclear about why the Xcode-compiled executable behaves differently. I have always had the input file in the same directory as the executable. Indeed there seems to be a relative vs. absolute path issue, but I can't diagnose where. The path specified under Project > Edit Active Executable is an absolute path. However, the program works normally when the full path to the file is specified when the open call is made.

The only other thing I've found is that if I navigate to the executable through the terminal, and execute it there, it works. However, if I navigate via the finder and execute it there, or execute from within Xcode the input file can't be found.