Xcode 4 中的命令行工具 Build for Release,在构建可执行文件中包含图像时出现问题
(首先,非常感谢。Stackoverflow 过去对我很有帮助!)
好的,我目前正在通过 Xcode 开发一个用 C++ 编写的命令行工具。 我过去已经做过十几个这样的程序,并且在 Xcode 内部或外部编译它们都没有问题。通常,我只是使用终端的 g++ 行手动编译,但我偶尔也会使用 Xcode 的版本。然而,与使用 g++ 相比,我对通过 Xcode 执行此操作肯定不太熟悉。
也就是说,这次我也尝试使用 SDL 框架构建一个项目。这是我第一次尝试任何类型的 GUI,它比我想象的要困难得多。我希望能够编译该项目并将可执行文件的副本发送给我的教授以展示我的进度,但我不知道该怎么做。
我认为不可能通过 g++ 构建,因为我还需要包含 SDL 和 Cocoa 框架。 (这可能是非常错误的,如果是这样,这也是一个选择!) 然而,当我通过 Xcode 构建时,我可以成功打开它并通过终端在外部运行它。所以我们很接近。但它无法找到我使用的任何图像。 SDL 绝对有效,我可以单击图像应该在的位置(并且它有效),但它只是说它无法打开“images/blahblah.bmp”。
目前我的文件夹层次结构是这样的:
项目文件夹包含我所有的 .cpp 和 .h 以及一个标题为“images”的文件夹,其中包含图像。例如,在我的所有 C++ 文件中,我将它们称为“images/border.bmp”。为了使其正常工作,我相信我必须将工作目录设置为该项目文件夹。所以这可能与问题有关。我通过当前方案在“工作目录”下做到了这一点。
我想也许图像文件夹没有包含在构建中?所以我尝试将其添加到目标中。 在“项目”、“目标”(通过在导航器中单击“我的项目”进行访问)下,我将文件夹添加到“编译源”、“将二进制文件与库链接”和“复制文件”,但没有结果。我分别单独和一起尝试过。
所以,我确信我错过了一些非常小而愚蠢的事情;我以前从未做过这样的事情。总得学点什么吧?但我在网上找不到有同样问题的人。 (另外,我的很多搜索结果都是博客,但我目前住在中国,无法访问其中的许多内容。...:-/)
非常感谢您的帮助!希望我已经说得足够清楚了!
简要更新: 有趣的是,我关闭了自定义工作目录,并将图像放入标准工作目录中(我很确定它就在“调试”或“发布”文件夹中的可执行文件旁边),但它仍然没有工作。不过它在 Xcode 中仍然可以工作(这告诉我我认为我的工作目录是正确的。)
(First off, thanks so much. Stackoverflow has been very helpful to me in the past!)
Okay, so I am currently working on a Command Line Tool written in C++ through Xcode.
I've done a dozen or so programs in the past like this and had no problem compiling them inside or outside of Xcode. Usually, I just use the terminal's g++ line to compile manually, but I have occasionally used Xcode's release. I am definitely less familiar with doing it through Xcode, however, than I am with g++.
That said, this time I am trying to build a project with the SDL framework as well. It's my first attempt at any sort of GUI and it is way tougher than I'd imagined. I wanted to be able to compile the project and send a copy of the executable file to my professor to show my progress, but I cannot figure out how to do it.
I don't think it's possible to build through g++ because I need to also include the SDL and Cocoa frameworks. (That could be very wrong, and if so, that's also an option!)
When I Build through Xcode, however, I can successfully open it and run it externally through Terminal. So we're close. But it is unable to find any of the images that I use.
The SDL definitely works, and I can click where the images should be (and it works) but it just says that it can't open "images/blahblah.bmp".
Currently my folder heirarchy is like this:
Project folder contains all my .cpp's and .h's as well as a folder titled "images" which contains the images. In all my c++ files, I refer to them as "images/border.bmp" for example. To get that to work, I believe I had to set my working directory to this project folder. So that may have something to do with the problem. I did that through the current Scheme, under "working directory".
I thought that maybe the images folder wasn't being included in the build? So I tried adding it to the target.
Under Project, Target (accessed by clicking on my Project in the Navigator) I added the folder to Compile Sources, Link Binaries with Libraries, and Copy Files with no result. I tried each independently as well as together.
So, I'm sure I'm missing something very small and silly; I've just never done anything like this before. Gotta learn somehow? But I wasn't able to find anyone with the same problem online. (also, a lot of my search results are blogs, but I'm currently staying in China and am unable to access many of them. ... :-/ )
Thanks so much for any help! Hopefully I've been clear enough!
Brief Update:
Interestingly, I turned off custom working directory, and put the images into the standard Working Directory instead(which I'm pretty sure is just next to the executable in either "Debug" or "Release" folder), and it still doesn't work. It does still work in Xcode though (which tells me i think I have the working directory correct.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mac 版本的 SDL 将工作目录设置为应用程序包的父目录。您没有创建 Mac 应用程序包,因此操作系统无法找到图像。短期修复方法是打开文件 SDLMain.m 并修改 setupWorkingDirectory: 方法,以便将工作目录设置为您的项目文件夹。
一个长期且更好的解决方案是创建 Mac 应用程序包。最简单的方法是使用 SDL 的 Xcode 项目模板。 SDL 项目模板不适用于 Xcode 4,因此您必须安装 Xcode 3.2,在 3.2 中创建项目,然后在 Xcode 4 中完成其余工作。创建 Mac 应用程序包的另一种方法是创建 Cocoa应用程序并将 SDL 框架、SDLMain.h 和 SDLMain.m 添加到您的项目中。您还必须删除一些文件,包括 xib 文件和 main.m 文件。
以下博客文章提供了有关在 Mac OS X 上使用 SDL 的提示:
适用于 Mac OS X 的 SDL 提示
The Mac version of SDL sets the working directory to the application bundle's parent directory. You're not creating a Mac application bundle so the operating system can't find the images. A short term fix would be to open the file SDLMain.m and modify the setupWorkingDirectory: method so the working directory is set to your project folder.
A long term and better fix is to create a Mac application bundle. The easiest way to do this is to use SDL's Xcode project templates. The SDL project templates don't work on Xcode 4 so you would have to install Xcode 3.2, create the project in 3.2, and do the rest of your work in Xcode 4. Another way to create a Mac application bundle is to create a Cocoa application and add the SDL framework, SDLMain.h, and SDLMain.m to your project. You also have to remove some files, including the xib file and the main.m file.
The following blog post has tips on working with SDL on Mac OS X:
SDL Tips for Mac OS X