<i id='4HMp0'><tr id='4HMp0'><dt id='4HMp0'><q id='4HMp0'><span id='4HMp0'><b id='4HMp0'><form id='4HMp0'><ins id='4HMp0'></ins><ul id='4HMp0'></ul><sub id='4HMp0'></sub></form><legend id='4HMp0'></legend><bdo id='4HMp0'><pre id='4HMp0'><center id='4HMp0'></center></pre></bdo></b><th id='4HMp0'></th></span></q></dt></tr></i><div id='4HMp0'><tfoot id='4HMp0'></tfoot><dl id='4HMp0'><fieldset id='4HMp0'></fieldset></dl></div>
      <tfoot id='4HMp0'></tfoot>

        <bdo id='4HMp0'></bdo><ul id='4HMp0'></ul>
      1. <small id='4HMp0'></small><noframes id='4HMp0'>

        <legend id='4HMp0'><style id='4HMp0'><dir id='4HMp0'><q id='4HMp0'></q></dir></style></legend>

        相对路径在 Xcode C++ 中不起作用

        Relative Paths Not Working in Xcode C++(相对路径在 Xcode C++ 中不起作用)

        <small id='LTEUx'></small><noframes id='LTEUx'>

            <tbody id='LTEUx'></tbody>
        1. <i id='LTEUx'><tr id='LTEUx'><dt id='LTEUx'><q id='LTEUx'><span id='LTEUx'><b id='LTEUx'><form id='LTEUx'><ins id='LTEUx'></ins><ul id='LTEUx'></ul><sub id='LTEUx'></sub></form><legend id='LTEUx'></legend><bdo id='LTEUx'><pre id='LTEUx'><center id='LTEUx'></center></pre></bdo></b><th id='LTEUx'></th></span></q></dt></tr></i><div id='LTEUx'><tfoot id='LTEUx'></tfoot><dl id='LTEUx'><fieldset id='LTEUx'></fieldset></dl></div>
          • <bdo id='LTEUx'></bdo><ul id='LTEUx'></ul>

                <legend id='LTEUx'><style id='LTEUx'><dir id='LTEUx'><q id='LTEUx'></q></dir></style></legend>
                  <tfoot id='LTEUx'></tfoot>
                  本文介绍了相对路径在 Xcode C++ 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  网上有很多帖子详细说明了相对路径在 Xcode 中是如何不起作用的.我确实有一个 Xcode 模板,我下载了它的相对路径可以在其中工作,但是我无法弄清楚为什么也无法在其他项目中复制它.

                  There are numerous post over the net that detail how relative paths don't work in Xcode. I do have an Xcode template that I downloaded where the relative paths DO work, however I have not been able to figure out why nor replicate it in other projects.

                  首先,我在 Xcode 3.1 中使用 C++.我没有使用 Objective-C,也没有使用任何 Cocoa/Carbon 框架,只是使用纯 C++.

                  Firstly, I am using C++ in Xcode 3.1. I am not using Objective-C, nor any Cocoa/Carbon frameworks, just pure C++.

                  这是适用于我的其他 Xcode 模板的代码:

                  Here is the code that works in my other Xcode template:

                  sound->LoadMusic( (std::string) "Resources/Audio/Pop.wav" );
                  

                  这个相对路径在 Windows 中也适用于我.运行以下命令为我提供了应用程序完整路径的绝对路径:

                  This relative path works for me also in Windows. Running the following command gives me an absolute path to the application's full path:

                  std::cout << "Current directory is: " << getcwd( buffer, 1000) << "
                  ";
                  

                  /应用程序/myApp

                  /Applications/myApp

                  我们如何获得在 Xcode .app 包中工作的相对路径?

                  How can we get relative paths to work in an Xcode .app bundle?

                  推荐答案

                  我花了大约 5 个小时的谷歌和尝试不同的东西终于找到了答案!

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

                  我抛出了一些额外的包含守卫,因为这使得它只能编译 Apple(我开发跨平台)并使代码更好.

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

                  我感谢其他 2 个人的回答,您的帮助最终让我走上了正确的道路,找到了这个答案,所以我已经投票给你们俩了.谢谢大家!!!!

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

                  这篇关于相对路径在 Xcode C++ 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                    <bdo id='zR4cK'></bdo><ul id='zR4cK'></ul>
                      <tbody id='zR4cK'></tbody>

                      <legend id='zR4cK'><style id='zR4cK'><dir id='zR4cK'><q id='zR4cK'></q></dir></style></legend>

                      • <i id='zR4cK'><tr id='zR4cK'><dt id='zR4cK'><q id='zR4cK'><span id='zR4cK'><b id='zR4cK'><form id='zR4cK'><ins id='zR4cK'></ins><ul id='zR4cK'></ul><sub id='zR4cK'></sub></form><legend id='zR4cK'></legend><bdo id='zR4cK'><pre id='zR4cK'><center id='zR4cK'></center></pre></bdo></b><th id='zR4cK'></th></span></q></dt></tr></i><div id='zR4cK'><tfoot id='zR4cK'></tfoot><dl id='zR4cK'><fieldset id='zR4cK'></fieldset></dl></div>
                        1. <small id='zR4cK'></small><noframes id='zR4cK'>

                            <tfoot id='zR4cK'></tfoot>