【问题标题】:why isn't my DEBUG preprocessor macro being recognized objective-c为什么我的 DEBUG 预处理器宏没有被识别objective-c
【发布时间】:2014-05-13 10:18:38
【问题描述】:

我接手的项目有多个构建方案:演示、发布、调试和生产。在整个代码中.. 有几个预处理器宏 if 语句,即

#ifdef DEMO
static NSString *const URL_SMART_TAXI      = @"http://demo.theapp.com";
#elif  PRODUCTION
static NSString *const URL_SMART_TAXI      = @"http://prod.theapp.com";
#elif  DEBUG
static NSString *const URL_SMART_TAXI      = @"http://localhost:8000";
#else
static NSString *const URL_SMART_TAXI      = @"http://dev.theapp.com";
#endif

由于某种原因,当我使用 demo 方案或 生产 方案进行构建时,这总是有效的。但它不适用于 调试(每当我更改方案并运行调试.. 它总是跳过调试并使用通配符选项)..

我查看了整个项目,我没有看到为演示或生产提供的任何特殊处理,而这些处理不是为调试提供的。

如果我运行grep -nri %environment% *,结果如下:

grep -nri production *
project.pbxproj:2767:               84380FEB1705D3E40085487D /* Production */ = { 
project.pbxproj:2797:                       name = Production;
project.pbxproj:2799:               84380FEC1705D3E40085487D /* Production */ = { 
project.pbxproj:2832:                                       "-DPRODUCTION",
project.pbxproj:2846:                       name = Production;
project.pbxproj:3013:                               84380FEB1705D3E40085487D /* Production */, 
project.pbxproj:3024:                               84380FEC1705D3E40085487D /* Production */, 
xcshareddata/xcschemes/theApp.xcscheme:47:      buildConfiguration = "Production"

grep -nri demo *
project.pbxproj:2685:               6314932116E4F7D000B351CA /* Demo */ = { 
project.pbxproj:2715:                       name = Demo;
project.pbxproj:2717:               6314932216E4F7D000B351CA /* Demo */ = { 
project.pbxproj:2751:                                       "-DDEMO",
project.pbxproj:2765:                       name = Demo;
project.pbxproj:3012:                               6314932116E4F7D000B351CA /* Demo */, 
project.pbxproj:3023:                               6314932216E4F7D000B351CA /* Demo */, 
xcshareddata/xcschemes/theApp.xcscheme:87:      buildConfiguration = "Demo"

grep -nri debug *
project.pbxproj:2848:               847D410E168CBD3700CE1B96 /* Debug */ = { 
project.pbxproj:2863:                                       "DEBUG=1",
project.pbxproj:2879:                       name = Debug;
project.pbxproj:2912:               847D4111168CBD3700CE1B96 /* Debug */ = { 
project.pbxproj:2955:                       name = Debug;
project.pbxproj:2972:                                       "DEBUG=1",
project.pbxproj:3010:                               847D410E168CBD3700CE1B96 /* Debug */, 
project.pbxproj:3021:                               847D4111168CBD3700CE1B96 /* Debug */, 
xcshareddata/xcschemes/theApp.xcscheme:26:      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
xcshareddata/xcschemes/theApp.xcscheme:27:      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
xcshareddata/xcschemes/theApp.xcscheme:29:      buildConfiguration = "Debug">
xcshareddata/xcschemes/theApp.xcscheme:43:      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
xcshareddata/xcschemes/theApp.xcscheme:44:      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
xcshareddata/xcschemes/theApp.xcscheme:49:      debugDocumentVersioning = "YES"
xcshareddata/xcschemes/theApp.xcscheme:72:      debugDocumentVersioning = "YES">
xcshareddata/xcschemes/theApp.xcscheme:84:      buildConfiguration = "Debug">

有什么想法吗?

更新: + 构建设置的相关部分

【问题讨论】:

  • 您在DEBUG=1 之前缺少-D - 它应该是-DDEBUG=1
  • 那个额外的D的目的是什么?
  • 这是一个用于定义宏的命令行开关 - 您可以在 PRODUCTION 和 DEMO 中使用它,但在 DEBUG 的情况下则缺少它。
  • @PaulR 但 DEBUG=1 是在我的 release 方案中定义的。我从不使用它。我应该将 -DNS_BLOCK_ASSERTIONS=1 -DDEBUG 放在其他 C/C++ 标志下吗?但DNS_BLOCK_ASSERTIONS 似乎与我正在尝试做的事情无关
  • 哦,我的坏..这两个是完全不同的东西..

标签: ios objective-c xcode macros c-preprocessor


【解决方案1】:

这是因为您正在执行“#elif”,这与“#elifdef是同一件事(如果存在这样的事情)。

您应该同时定义 PRODUCTION、DEBUG 和 DEMO,但只将其中一个设置为“1”,其他设置为“0”。

【讨论】:

  • 那么为什么当我使用#elif 进行生产时它工作得很好?
  • @Michael 是对的,您不应该将#ifdef#elif 混合使用,但我认为您的真正问题是在DEBUG=1 之前缺少-D
  • 因为您可能正在执行“#define PRODUCTION 1”,并且由于在为生产构建时根本没有定义 DEBUG,所以它与您执行“#if PRODUCTION”一样匹配。编译时会变成“#if 1”。
  • @PaulR 好吧,伙计们,我看到你们所说的很有价值。但我还是有点困惑。我用构建设置的相关部分更新了我的问题。 . 是不是让我的情况更清楚了?
  • 如果这是我的项目,我可能将“DEMO=0 PRODUCTION=0”添加到显示“DEBUG=1”的预处理器宏行中。然后将“#ifdef”更改为“#if”。现在有意义吗?
猜你喜欢
  • 2012-09-28
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 2021-02-22
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
相关资源
最近更新 更多