objective-c 如何在 Xcode 中定义预处理器符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/367368/
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-03 20:59:24  来源:igfitidea点击:

How to define a preprocessor symbol in Xcode

objective-cxcodec-preprocessor

提问by Steph Thirion

Is it possible to set a symbol for conditional compilation by setting up properties in an Xcode project?

是否可以通过在 Xcode 项目中设置属性来设置条件编译的符号?

My aim is to to create a symbol that is available to all files, without having to use import/include, so that a set of common classes can have a special behavior in some projects. Like the following, but with my own symbols.

我的目标是创建一个对所有文件都可用的符号,而不必使用导入/包含,以便一组公共类可以在某些项目中具有特殊行为。像下面这样,但有我自己的符号。

#if TARGET_IPHONE_SIMULATOR
    ...
#endif

回答by Ben Gottlieb

Go to your Target or Project settings, click the Gear icon at the bottom left, and select "Add User-Defined Setting". The new setting name should be GCC_PREPROCESSOR_DEFINITIONS, and you can type your definitions in the right-hand field.

转到您的目标或项目设置,单击左下角的齿轮图标,然后选择“添加用户定义的设置”。新设置名称应为GCC_PREPROCESSOR_DEFINITIONS,您可以在右侧字段中输入您的定义。

Per Steph's comments, the full syntax is:

根据 Steph 的评论,完整的语法是:

constant_1=VALUE constant_2=VALUE

Note that you don't needthe '='s if you just want to #define a symbol, rather than giving it a value (for #ifdefstatements)

请注意,如果您只想#define 一个符号,而不是给它一个值(用于语句),则不需要'='s#ifdef

回答by cdespinosa

You don't need to create a user-defined setting. The built-in setting "Preprocessor Macros" works just fine. alt text http://idisk.mac.com/cdespinosa/Public/Picture%204.png

您不需要创建用户定义的设置。内置设置“预处理器宏”工作正常。 替代文字 http://idisk.mac.com/cdespinosa/Public/Picture%204.png

If you have multiple targets or projects that use the same prefix file, use Preprocessor Macros Not Used In Precompiled Headers instead, so differences in your macro definition don't trigger an unnecessary extra set of precompiled headers.

如果您有多个使用相同前缀文件的目标或项目,请改用未在预编译头文件中使用的预处理器宏,这样宏定义中的差异就不会触发一组不必要的额外预编译头文件。

回答by Stickley

As an addendum, if you are using this technique to define strings in your target, this is how I had to define and use them:

作为附录,如果您使用这种技术在目标中定义字符串,那么我必须这样定义和使用它们:

In Build Settings -> Preprocessor Macros, and yes backslashes are critical in the definition:

在 Build Settings -> Preprocessor Macros 中,是的反斜杠在定义中很重要:

APPURL_NSString=\@\"www.foobar.org\"

And in the source code:

在源代码中:

objectManager.client.baseURL = APPURL_NSString;

回答by chunkyguy

You can use the *_Prefix.pchfile to declare project wide macros. That file is usually in you Other Sourcesgroup.

您可以使用该*_Prefix.pch文件来声明项目范围的宏。该文件通常在您的其他来源组中。

回答by hEADcRASH

It's under "GCC 4.2 Preprocessing" (or just put "prepro" in the search box)...

它在“GCC 4.2 Preprocessing”下(或者只是在搜索框中输入“prepro”)......

...however, for the life of me I can't get it to work.

......但是,对于我的生活,我无法让它发挥作用。

I have my standard Debug and Release configurations, and I want to define DEBUG=1 in the debugging configuration. But after adding it as a value:

我有我的标准调试和发布配置,我想在调试配置中定义 DEBUG=1。但是在将其添加为值后:

(in the settings window) > Preprocessor Macros : DEBUG=1

(在设置窗口中)> 预处理器宏:DEBUG=1

#if DEBUG
    printf("DEBUG is set!");
#endif 

...never prints/gets called. It's driving me crazy...

...从不打印/被调用。这让我疯狂...

回答by Mark24x7

In response to Kevin Laity's comment (see cdespinosa's answer), about the GCC Preprocessing section not showing in your build settings, make the Active SDK the one that says (Base SDK) after it and this section will appear. You can do this by choosing the menu Project > Set Active Target > XXX (Base SDK). In different versions of XCode (Base SDK) maybe different, like (Project Setting or Project Default).

为了回应 Kevin Laity 的评论(请参阅 cdespinosa 的回答),关于 GCC 预处理部分未显示在您的构建设置中,请将 Active SDK 设为在其后面显示 (Base SDK) 的内容,此部分将出现。您可以通过选择菜单 Project > Set Active Target > XXX (Base SDK) 来实现。在不同版本的 XCode(基础 SDK)中可能会有所不同,例如(项目设置或项目默认)。

After you get this section appears, you can add your definitions to Processor Macros rather than creating a user-defined setting.

出现此部分后,您可以将定义添加到处理器宏,而不是创建用户定义的设置。

回答by Petr Javorik

For Xcode 9.4.1 and C++ project. Adding const char*Preprocessor Macros to both Debugand Releasebuilds.

对于 Xcode 9.4.1 和 C++ 项目。将const char*预处理器宏添加到调试发布版本。

  1. Select your project

    select project

  2. Select Build Settings

    select build settings

  3. Search "Preprocessor Macros"

    search1search2

  4. Open interactive list

    open interactive list

  5. Add your macros and don't forget to escape quotation

    add path

  6. Use in source code as common const char*

    ...
    #ifndef JSON_DEFINITIONS_FILE_PATH
    static constexpr auto JSON_DEFINITIONS_FILE_PATH = "definitions.json";
    #endif
    ...
    FILE *pFileIn = fopen(JSON_DEFINITIONS_FILE_PATH, "r");
    ...
    
  1. 选择您的项目

    选择项目

  2. 选择构建设置

    选择构建设置

  3. 搜索“预处理器宏

    搜索1搜索2

  4. 打开互动列表

    打开互动列表

  5. 添加您的宏,不要忘记转义引号

    添加路径

  6. 在源代码中常用 const char*

    ...
    #ifndef JSON_DEFINITIONS_FILE_PATH
    static constexpr auto JSON_DEFINITIONS_FILE_PATH = "definitions.json";
    #endif
    ...
    FILE *pFileIn = fopen(JSON_DEFINITIONS_FILE_PATH, "r");
    ...
    

回答by kslcam

You can duplicate the target which has the preprocessing section, rename it to any name you want, and then change your Preprocessor macro value.

您可以复制具有预处理部分的目标,将其重命名为您想要的任何名称,然后更改您的预处理器宏值。