0

I'm writing a little C++ program in Xcode 5 for fun, but it's my first time using Xcode and I'm confused as to how Xcode handles paths when opening files that are included in the project. I'm using the SDL2_image framework and when I want to load an image that I've included in the project I just do this and it finds the image just fine:

IMG_LoadTexture(renderer, "image_name.png");

However, this doesn't seem to work anywhere else. I'm trying to load an xml file with libxml2 but this code:

xmlDocPtr doc = xmlParseFile("doc_name.xml");

Just results in this:

I/O warning : failed to load external entity "doc_name.xml"

Similar things happen when I try other methods of loading files. I'm not sure if this is because the SDL2_Image framework is doing something special, or because I have some other fundamental misunderstanding of how things work. All of the files should be included the same way, I just right clicked on the project and selected "Add files to...". When compiled, they show up in the application's "Contents/Resources" folder. I have seen other questions on stackoverflow about how to open files in the bundle (which I assume is the contents folder?), but I'm confused as to why I didn't have to do anything that those answers suggest to make it work with the SDL_Image stuff.

I'd greatly appreciate any help with this. I've fixed all my problems so far with magical build settings from the internet that I don't understand, so any explanation that shows how I could have figured this out for myself so that next time I can would be greatly appreciated.

[UPDATE] If I use the path "Application_Name.app/Contents/Resources/file_name" I can access the file. However I don't understand why I have to do this with everything except for the SDL2_Image library, and I don't want my paths to depend on the name of the application.

Mason
  • 33
  • 1
  • 5

1 Answers1

1

Option 1) I think the problem here is not with the C++ code, but with the file location. In XCode, binary programs are built in the Executables file location. You have to set up the build phases for copying your input file to the Executables location via "Copy Bundle Resources". See Apple's Documentation

Option 2) Alternatively you could work with hard coded paths and create an installer that is quickly done in bash, and a script run can be easily implemented in the XCode scheme editor with each phase.

Option 3) Use CoreFoundation and compiler macro like this: Relative Paths Not Working in Xcode C++

Community
  • 1
  • 1
Jeremiah Smith
  • 740
  • 6
  • 17
  • Thanks--unfortunately there's already a Copy Bundle Resources build phase there by default with all the files I'm trying to access in it, if that's the case I shouldn't have to change that at all should I? – Mason Dec 01 '13 at 19:52
  • If it works, all is fine, as I explained the executable's location is the key, alternatively you could work with hard coded paths and create an installer that is quickly done in bash, and a script run can be easily implemented in the XCode scheme editor with each phase. – Jeremiah Smith Dec 01 '13 at 20:05
  • It works in the sense that it's putting the files in the Contents->Resources folder of the compiled app, but my code still can't find the file. – Mason Dec 01 '13 at 22:54
  • I added an update to my question with some more information at the bottom – Mason Dec 01 '13 at 23:06
  • Ammended the answer to compiler macro with CoreFoundation – Jeremiah Smith Dec 02 '13 at 06:41
  • Thank you so much! That fixed it for me. I've been messing with CoreFoundation/Foundation for hours trying to do exactly what that answer describes, you just saved me a lot of frustration. I apologize for being unclear in my original question, now that I see what the issue was I see that a lot of the information I gave was irrelevant. – Mason Dec 02 '13 at 07:24