I am working on figuring out how to use Xcode 4 to debug c++ projects.
我正在研究如何使用Xcode 4调试c++项目。
I have basically copy pasted a working c++ executable that when compiled from the terminal ran fine.
我基本上复制了一个正在运行的c++可执行文件,当从终端编译时,它运行良好。
However, i was thinking it might be nice to use Xcode for debugging. So I am trying to migrate the single .cpp file into Xcode as a command line tool.
但是,我认为使用Xcode进行调试可能比较好。因此,我试图将单个.cpp文件作为命令行工具迁移到Xcode中。
I need to read in a file called numbers.txt (which I supply through a command line argument) which is located in my project directory, and then out put to a file (whose name I also specify as an argument.)
我需要读一个叫做数字的文件。txt(我通过命令行参数提供)位于我的项目目录中,然后输出到一个文件(我还将其名称指定为参数)。
The problem I am running into is that the files that are supplied as command line arguments are failing to open.
我遇到的问题是作为命令行参数提供的文件未能打开。
ifstream in;
ofstream out;
in.open(argv[1]);
out.open(argv[2]);
I have checked to make sure that the arguments are being properly passed and are named correctly. The ifstream in
is being supplied with `numbers.txt', which I want to open a text file that I already have.
我检查了参数是否被正确地传递和命名。ifstream in正在提供“号码”。txt',我想打开一个我已经有的文本文件。
However when I check to make sure the ifstream
is open:
但是,当我检查以确保ifstream是打开的时候:
if(in.is_open() == false){
cerr << "Unable to open input file" << endl;
return 1;
}
I get the error.
我得到错误。
I suspect this has something to do with how Xcode organizes the project.
我怀疑这与Xcode组织项目的方式有关。
my numbers.txt
file is just sitting in the Xcode project folder, and I have only one .cpp
class and one product, the executable.
我的数字。txt文件只存在于Xcode项目文件夹中,而我只有一个。cpp类和一个产品,可执行文件。
anyone know what I am missing here?
有人知道我错过了什么吗?
2 个解决方案
#1
3
The executable built by Xcode is in a different folder than the project. Passing in the name of the file without an absolute path before it will cause the executable to look for it in the wrong place, which is why it can't be found. Some of the possible solutions are to include the file as part of the build process (so it ends up in the same directory as the executable) or to pass the file to be opened by its absolute path. There are other ways to solve the problem, too, but hopefully that should be enough to get you started.
Xcode构建的可执行文件在与项目不同的文件夹中。在没有绝对路径的情况下传入文件的名称将导致可执行文件在错误的位置查找它,这就是为什么无法找到它。一些可能的解决方案是将文件包含为构建过程的一部分(因此它最终与可执行文件位于同一个目录中),或者传递由其绝对路径打开的文件。也有其他方法可以解决这个问题,但希望这足够让你开始。
#2
2
Old thread, but i have faced the same problem now, and it is easy to solve. Just copy the file in the build phase. Here is an screenshot of the final result (note the destination, subpath and checkbox):
老线程,但我现在也遇到了同样的问题,很容易解决。在构建阶段复制文件即可。以下是最终结果的截图(注意目的地、子路径和复选框):
#1
3
The executable built by Xcode is in a different folder than the project. Passing in the name of the file without an absolute path before it will cause the executable to look for it in the wrong place, which is why it can't be found. Some of the possible solutions are to include the file as part of the build process (so it ends up in the same directory as the executable) or to pass the file to be opened by its absolute path. There are other ways to solve the problem, too, but hopefully that should be enough to get you started.
Xcode构建的可执行文件在与项目不同的文件夹中。在没有绝对路径的情况下传入文件的名称将导致可执行文件在错误的位置查找它,这就是为什么无法找到它。一些可能的解决方案是将文件包含为构建过程的一部分(因此它最终与可执行文件位于同一个目录中),或者传递由其绝对路径打开的文件。也有其他方法可以解决这个问题,但希望这足够让你开始。
#2
2
Old thread, but i have faced the same problem now, and it is easy to solve. Just copy the file in the build phase. Here is an screenshot of the final result (note the destination, subpath and checkbox):
老线程,但我现在也遇到了同样的问题,很容易解决。在构建阶段复制文件即可。以下是最终结果的截图(注意目的地、子路径和复选框):