Xcode中的条件环境变量 [英] Conditional Environment Variables in Xcode

查看:148
本文介绍了Xcode中的条件环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  NSDebugEnabled 
NSZombieEnabled
MallocStackLogging
MallocStackLoggingNoCompact

是否可以创建一个新的构建配置复制Debug,那些环境变量设置为YES,但是我可以直接切换到常规Debug,那些将被关闭?





解决方案

IIRC,环境变量设置在可执行文件中,而不是配置/目标(如果我错了,忽略这个错误)。您可能可以使用Xcode变量替换:




  • 创建一个名为NSDebugEnabled的布尔构建设置。将NSDebugEnabled环境变量设置为$(NSDebugEnabled)。 (它可能必须是一个值为YES的字符串;我不知道NS环境变量是多么挑剔。)

  • 创建一个名为NSDebugEnabled的字符串构建设置。将其设置为NSDebugEnabled以启用和其他(空字符串?)禁用。使用$(NSDebugEnable)作为环境变量名,YES作为值。



如果变量替换不起作用,可以在main()的开头调用setenv()/ putenv()(在分配自动释放池之前)在变量读取之前改变环境;这是否成功取决于阅读时间。在第一次调用malloc()时,malloc()读取它们(由于Obj-C运行时/ +加载方法,这可能非常早)。我不知道什么时候读取NS *。



如果仍然不起作用,而且您在开始运行 em>,我想可以使用execve():

  #include< unistd.h> ; 

int main(int argc,char ** argv,char ** envp)
{
#ifdef DEBUGENVIRON
if(!getenv(NSDebugEnabled))
{
setenv(NSDebugEnabled,1,1);
...设置其他变量...
//也许这会抱怨自动释放池。
char * executablePath = [[[NSBundle mainBundle] executablePath] filesystemRepresentation];
execve(executablePath,argv,environ); abort();
}
#endif
...做你通常在main()...
}

我很确定设备上的沙盒禁止执行execve()系统调用。






如果这太多了,这就是我所做的:



将一些环境变量添加到可执行文件中。取消选中该复选框。需要调试时,勾选复选框。调试完成后,取消选中。在提交之前,请检查您是否在.xcodeproj中没有任何愚蠢的事情。



如果您使用漏洞(泄漏应自动设置),我认为您不需要MallocStackLogging )



您还可以考虑使用MallocPreScribble和MallocScribble。


I would like to create some environment viables in Xcode for heavy debugging such as:

  NSDebugEnabled
  NSZombieEnabled
  MallocStackLogging
  MallocStackLoggingNoCompact

Is it possible to create a new build configuration that is a duplicate of "Debug" where those environmental variables are set to YES, but i can just switch to regular Debug and those would be off again?


解决方案

IIRC, Environment variables are set in the "executable", not the configuration/target (ignore this rant if I'm wrong). You might be able to use Xcode variable substitution:

  • Create a boolean build setting called "NSDebugEnabled". Set the NSDebugEnabled environment variable to $(NSDebugEnabled). (It might have to be a string with the value YES; I'm not sure how picky the NS environment variables are.)
  • Create a string build setting called "NSDebugEnabled". Set it to "NSDebugEnabled" to enable and something else (the empty string?) to disable. Use $(NSDebugEnable) as the environment variable name and "YES" as the value.

If variable substitution doesn't work, you might be able to call setenv()/putenv() at the very start of main() (before allocating an autorelease pool) to alter the environment before the variables are read; whether this is successful depends on when they're read. IIRC malloc() reads them the first time you call malloc() (this might be very early due to Obj-C runtime/+load methods). I'm not sure when the NS* ones are read.

If that still doesn't work, and you're happy running in the simulator, I think you can use execve():

#include <unistd.h>

int main (int argc, char ** argv, char ** envp)
{
#ifdef DEBUGENVIRON
  if (!getenv("NSDebugEnabled"))
  {
    setenv("NSDebugEnabled","1",1);
    ... set the other variables ...
    // Maybe this will complain about an autorelease pool.
    char * executablePath = [[[NSBundle mainBundle] executablePath] filesystemRepresentation];
    execve(executablePath, argv, environ); abort();
  }
#endif
  ... do what you normally do in main() ...
}

I'm pretty sure the execve() system call is prohibited by sandboxing on device.


If all that's too much, here's what I do:

Add some environment variables to the executable. Uncheck the checkbox. When debugging is required, check the checkboxes. When debugging is finished, uncheck them. Before committing, check that you aren't committing anything silly in the .xcodeproj.

I don't think you need MallocStackLogging if you use Leaks (Leaks should set it automatically).

You might also consider MallocPreScribble and MallocScribble.

这篇关于Xcode中的条件环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆