如何为 xcode 添加全局包含路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/749027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:59:55  来源:igfitidea点击:

How to add a global include path for xcode

xcodemacosinclude

提问by Jesse Beder

I'd like to add ~/include to my include path for allprojects while using Xcode, something like setting the environment variable CPLUS_INCLUDE_PATHin Linux. (See herefor the related Linux question.)

我想在使用 Xcode 时将 ~/include 添加到所有项目的包含路径中,例如CPLUS_INCLUDE_PATH在 Linux 中设置环境变量。(请参阅此处了解相关的 Linux 问题。)

Is this possible in Xcode? I tried setting the above environment variable, but it doesn't seem to work. (And if it is possible, I'd like to also set related paths, like LIBRARY_PATH and LD_LIBRARY_PATH.)

这在 Xcode 中可能吗?我尝试设置上述环境变量,但似乎不起作用。(如果可能的话,我还想设置相关路径,如 LIBRARY_PATH 和 LD_LIBRARY_PATH。)

采纳答案by Jesse Rusak

According to Apple's Docs, you are able to provide a default for any build setting using an environment variable.

根据Apple 的 Docs,您可以使用环境变量为任何构建设置提供默认

In this case, you'd want to set HEADER_SEARCH_PATHS. For some reason I don't understand, this doesn't work. It works fine for other build settings (for example, OTHER_CFLAGS), but not for HEADER_SEARCH_PATHS. You can see what the variable name for any setting is by opening the research assistant in the build settings window (book button on the bottom left.)

在这种情况下,您需要设置HEADER_SEARCH_PATHS. 出于某种原因,我不明白,这行不通。它适用于其他构建设置(例如 OTHER_CFLAGS),但不适用于HEADER_SEARCH_PATHS. 您可以通过在构建设置窗口(左下角的书本按钮)中打开研究助手来查看任何设置的变量名称。

One var that does work is USER_HEADER_SEARCH_PATHS, which is just like what you want, but only works for paths in double quotes (not in angle brackets.)

一个有效的 var 是USER_HEADER_SEARCH_PATHS,它就像你想要的那样,但只适用于双引号中的路径(不在尖括号中。)

So

所以

#include "bar.h"

would work, but

会工作,但是

#include <bar.h>

wouldn't.

不会。

The build settings plist referenced from the above article on environment variables should end up looking something like this:

从上面关于环境变量的文章中引用的构建设置 plist 最终应该看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>USER_HEADER_SEARCH_PATHS</key>
    <string>~/test</string>
</dict>
</plist>

Hope that helps.

希望有帮助。

回答by Alex Gray

For any of you blessed darlingsthat are trying to achieve this, or any otherhack of ~/.MacOSX/environment.plist, aka global environmental variablesin this post-Mountain-Lion world, check out EnvPane - An OS X preference pane for environment variables

对于任何试图实现这一目标的幸运宠儿,或在这个后山狮世界中~/.MacOSX/environment.plistaka全局环境变量的任何其他黑客攻击,请查看EnvPane - 环境变量的 OS X 首选项窗格

EnvPane is a preference pane for Mac OS X 10.8 (Mountain Lion) that lets you set environment variables for all programs in both graphical and terminal sessions. Not only does it restore support for ~/.MacOSX/environment.plist in Mountain Lion, it also publishes your changes to the environment immediately, without the need to log out and back in. This works even for changes made by manually editing ~/.MacOSX/environment.plist, not just changes made via the preference pane.

EnvPane 是 Mac OS X 10.8 (Mountain Lion) 的首选项窗格,可让您为图形和终端会话中的所有程序设置环境变量。它不仅在 Mountain Lion 中恢复对 ~/.MacOSX/environment.plist 的支持,它还可以立即将您的更改发布到环境中,而无需注销并重新登录。这甚至适用于通过手动编辑所做的更改 ~/ .MacOSX/environment.plist,而不仅仅是通过首选项窗格所做的更改。

enter image description here

在此处输入图片说明

回答by parceval

Just select a .c or .m file in your "Groups & Files Tab" and choose "get Info" through RMB, then under "build" you can add additional Compiler Flags like -I for additional include searchpath.

只需在“组和文件选项卡”中选择一个 .c 或 .m 文件,然后通过 RMB 选择“获取信息”,然后在“构建”下,您可以添加额外的编译器标志,如 -I 以获取额外的包含搜索路径。

回答by Jason Coco

Add the HEADER_SEARCH_PATHSbuild setting to your Xcode project. This setting takes a space separated list of paths and if the path ends in a double star (**), it will search subdirectories recursively. So to answer for your example, set HEADER_SEARCH_PATHSto:

HEADER_SEARCH_PATHS构建设置添加到您的 Xcode 项目中。此设置采用空格分隔的路径列表,如果路径以双星 ( **)结尾,它将递归搜索子目录。因此,要回答您的示例,请设置HEADER_SEARCH_PATHS为:

~/include

If you had a number of paths to include, you would set it to something like:

如果您有许多路径要包含,您可以将其设置为:

~/include ~/my_other_includes/** /usr/local/special_frameworks/**

回答by cdespinosa

Build settings are not environment variables, and environment variables are not build settings. Setting an environment variable will not affect Xcode builds.

构建设置不是环境变量,环境变量也不是构建设置。设置环境变量不会影响 Xcode 构建。

USER_HEADER_SEARCH_PATHSis a build setting, and putting a list of paths into it will achieve what you want. If you set the value in a target's inspector, it will take effect for only that target (and only for the Build Configurations you designate). If you set it in the Project inspector, it will take effect in all targets in the project, unless a target overrides it with its own setting.

USER_HEADER_SEARCH_PATHS是一个构建设置,将路径列表放入其中将实现您想要的。如果您在目标的检查器中设置该值,它将仅对该目标(并且仅对您指定的构建配置)生效。如果您在项目检查器中设置它,它将在项目中的所有目标中生效,除非目标使用自己的设置覆盖它。

For paths that are specific to your machine, you should probably define a Source Tree in the Xcode Preferences, such as LOCAL_INCLUDE = ~/include. Then define USER_HEADER_SEARCH_PATHS = $(LOCAL_INCLUDE)in the project. This way, other people can open your project and build it by setting their Source Tree to the particular location of the local includes on their machine, without having to change the project file.

对于特定于您的机器的路径,您可能应该在 Xcode Preferences 中定义一个 Source Tree,例如LOCAL_INCLUDE = ~/include. 然后USER_HEADER_SEARCH_PATHS = $(LOCAL_INCLUDE)在项目中定义。这样,其他人就可以打开您的项目并通过将他们的源树设置为他们机器上本地包含的特定位置来构建它,而无需更改项目文件。