gpt4 book ai didi

c++ - 获取 Mac .app 包中的相对路径

转载 作者:行者123 更新时间:2023-11-30 03:05:48 25 4
gpt4 key购买 nike

我已经用 C++、OpenGL 和 OpenAL 开发了一款游戏,我正在尝试将其构建到应用程序包中而不是命令行可执行文件中。我复制并粘贴了 Relative Paths Not Working in Xcode C++ 中的代码在我的 main.cpp 文件的顶部(也在 App_Prefix.pch 中尝试过),我得到了一些语法错误。我用错误注释了代码:

#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

我觉得他的代码很好,很多人都在他的帖子中感谢他。我做错了什么吗?我是否应该专门为此代码创建一个文件并在导入之前导入它?

最佳答案

您的代码应该在函数内部,例如 main()。目前您的代码是在文件级别粘贴的。那里不能有 if 语句或函数调用。

关于c++ - 获取 Mac .app 包中的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7461318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com