Relative Paths Not Working in Xcode C++

Relative Paths Not Working in Xcode C++

Took me about 5 hours of Google and trying different things to FINALLY find the answer!

#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif

// ----------------------------------------------------------------------------
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
char path[PATH_MAX];
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
{
// error!
}
CFRelease(resourcesURL);

chdir(path);
std::cout << "Current Path: " << path << std::endl;
#endif
// ----------------------------------------------------------------------------

I've thrown some extra include guards because this makes it compile Apple only (I develop cross platform) and makes the code nicer.

I thank the other 2 guys for your answers, your help ultimately got me on the right track to find this answer so i've voted you both up. Thanks guys!!!!

Cannot open file with relative path? (C++ ifstream)

Print out your current working directory when you run the program:

char buffer[256];
char *val = getcwd(buffer, sizeof(buffer));
if (val) {
std::cout << buffer << std::endl;
}

This will tell you where you are running your program from and thus why the path doesn't match for relative paths. A relative path is relative to the current working directory, not to where your binary is located.

If you want to make the path relative to the location of the binary then you will have to do that yourself. Many programming languages offer this as an option, but it is not built-in to C++. You can do this by finding the executable using the argv[0] from main. Then you need to drop the file component of the executable path and replace it with the file name that you are interested in.

Since C++17, you can use std::filesystem::current_path() instead of getcwd.

std::cout << std::filesystem::current_path() << std::endl;

Difficulty in using SDL_LoadBMP with relative path in Xcode

In your Build Phases you need to create a new build phase of type Copy Files.

Set Destination to be 'Resources' and leave Subpath blank unless you want to have a subfolder in your resources (can be useful when you have a lot and want to separate eg fonts, textures, sounds etc)

Leave Copy only when installing unticked

Then just add the resources to that build phase, in your case helloworld.bmp

You should then be able to load by doing:

helloWorld = SDL_LoadBMP("helloworld.bmp");

or if you used a Subpath:

helloWorld = SDL_LoadBMP("textures/helloworld.bmp");

This is the same sort of way you could bundle the SDL2.framework with your application. There is a tutorial here - http://zamma.co.uk/setup-sdl2-in-xcode-osx/ and in Part 3 it describes how to bundle the frameworks if you are interested.

Overal though the best way to do fully portable builds is going to be something like CMake or Gradle.

Can't get relative location of image to use, having to use absolute path

I worked it out.

set ImagePath to current application's NSBundle's mainBundle()'s bundlePath() as text & "/Contents/Resources/imageName.png"

Thanks.

Adding frameworks to project in Xcode 5 and having *relative* paths added

I faced the same issue. There is a simple fix. Go to framework search paths. Remove everything from there. Add ./ and make it recursive. That's it.

Xcode: How to set current working directory to a relative path for an executable?

You can use Xcode build setting variables such as PROJECT_DIR, e.g. setting your working directory to $PROJECT_DIR/.. will make it equal to the parent directory of your project directory.



Related Topics



Leave a reply



Submit