1

I've developed a game in C++, OpenGL and OpenAL and I am trying to build it into an app bundle rather than a command-line executable. I copied and pasted the code from Relative Paths Not Working in Xcode C++ at the top of my main.cpp file (also tried it in the App_Prefix.pch) and I get a number of syntax errors. I've annotated the code with the errors:

#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: expected unqualified-id before 'if'
    {
        // error!
    }
    CFRelease(resourcesURL); // error: expected constructor, destructor or type conversion before '(' token

    chdir(path); // error: expected constructor, destructor or type conversion before '(' token
    std::cout << "Current Path: " << path << std::endl; // error: expected constructor, destructor or type conversion before '<<' token
#endif

His code looks fine to me, and a lot of people have thanked him for it in his post. Is there anything I am doing wrong? Should I create a file specifically for this code and import it before anything else?

Community
  • 1
  • 1
benwad
  • 6,414
  • 10
  • 59
  • 93

1 Answers1

3

Your code should be inside a function, for example main(). Currently your code is pasted at the file level. You can't have if statement or function calls there.

Yuji
  • 34,103
  • 3
  • 70
  • 88