开发者

Getting relative paths within a Mac .app bundle

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

    chd开发者_C百科ir(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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜