Question

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.

Was it helpful?

Solution

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++

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top