1

In Xcode, whenever I want to use a file (in C or C++ specifically), I have to specify the full path to the file, whereas with gcc or g++, I do not have to if it is in the same directory as the code referencing it.

Is there a reason to why this is? And if so, is there a way to fix it?

4

3 回答 3

1

问题不在于“XCode”不接受相对路径。问题是您的代码具有与您想象的不同的当前工作目录。正如评论所说,您可以使用它chdir来到达您的文件所在的位置,或者使用考虑到当前工作目录的相对路径(您可以使用它getcwd()来获取当前工作目录)。

当您使用gccg++在命令行上时,可以控制最终应用程序在哪个目录中运行。

于 2013-09-13T17:01:25.110 回答
0

Click on the little folder icon above the Navigator, i.e., the panel at the left of the XCode window. This should let you see what files and products are part of your project. There is a folder that says "Products." Expand that folder by clicking on the triangle to its left. You will see your executable (the icon looks like a terminal window). Control-click on that and choose "Reveal in Finder" to open the folder that actually contains the executable. That's the directory you need your files to be in for XCode to find them the way GCC does.

于 2013-09-13T17:16:55.480 回答
0

原因是有一个“当前工作目录”的概念。所以你的 fopen()s 等从那里开始。在 unix 中,在 shell 中,CWD 是您的当前目录。从 MacOS 的终端窗口启动命令行工具也是如此。但是,当您从 Xcode 开始或启动应用程序时,CWD 就在其他地方。

就像我在评论中所说的那样,您可以调用chdir()将当前工作目录更改为您想要开始的位置。那么您不需要指定完整路径。

于 2013-09-13T17:03:37.107 回答