Bob Ippolito (@etrepum) on Haskell, Python, Erlang, JavaScript, etc.

Accessing environment information from bundles/dylibs (OS X)


The way that dylib works on OS X prevents you from directly referencing things defined in the executable from an external library (there are kludge linker flags, but I don't recommend their usage).

The way to do it is to use crt_externs.h. I didn't come up with this by the way, it's all over OS X (and NeXT, the header is from 1995) compatible software, such as Python and TCL (just google for _NSGetEnviron). You can even setup macros to make it feel like you have "unix-like" access to your environment, for example:

#if defined(__APPLE__)
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
extern char **environ;
#endif /* __APPLE __ */

It seems that a few projects have tried to do this the wrong way, such as 4Suite. They use one of the kludge linker flags and an extern pointer to environ, but that doesn't actually work because they missed a different kludge linker flag ;) It may have worked under some conditions, such as with Apple's Python 2.2.0, but nobody should've been using that anyway.

blog comments powered by Disqus