[PATCH] environ is not defined in the anonymo

  • Charles McGarvey

    The external variable `environ' is actually declared on line 422, within
    the stlplus namespace.  This bug causes compilation to fail with g++ 4.1.3
    on NetBSD 5.0.2.
    portability/subprocesses.cpp |    2 +-
    1 files changed, 1 insertions(+), 1 deletions(-)

    diff -git a/portability/subprocesses.cpp b/portability/subprocesses.cpp
    index 4f5c7eb..cf15b13 100644
    -- a/portability/subprocesses.cpp
    +++ b/portability/subprocesses.cpp
    @@ -498,7 +498,7 @@ namespace stlplus
         m_env = envp_copy(env);
    -    m_env = envp_copy(::environ);
    +    m_env = envp_copy(environ);

  • Andy Rushton

    Andy Rushton - 2010-06-09

    Could you say what the compiler error is please. Your solution is not a solution and I don't see how it can possibly link correctly.

    The environ variable is meant to be a global variable declared by the OS and should be in global scope, so ::environ is the correct name for it. Moving it into the stlplus space as you have done is wrong. See 'man environ' for information on this variable.

    I agree that having the extern declaration in stlplus namespace may not be correct (though there's been no error reports before), so it could be moved out of the namespace into global space. However, without the actual error messages that you are getting I can't really suggest a correction.

  • Charles McGarvey

    It works on platforms which declare extern char **environ for you in unistd.h, but that's not always the case.  Usually you have to declare it like you have done on line 422.  The only problem is you declare it in the stlplus namespace and reference it on line 501 using ::environ as if it was in the anonymous namespace.  In other words, your declaration on line 422 doesn't do anything because you're just referring to environ in the anonymous namespace and hoping it is declared in unistd.h.

    The compilation error is:

    src/stlplus/portability/subprocesses.cpp: In constructor 'stlplus::env_vector::env_vector()':
    src/stlplus/portability/subprocesses.cpp:501: error: '::environ' has not been declared

    Besides changing the reference on line 501, another solution would be to declare environ explicitly in an nameless namespace.

  • Andy Rushton

    Andy Rushton - 2010-06-09

    You've missed my point. Your solution does not work.

    In order for env_vector to initialise correctly from the current environment, the global ::environ must be used. Removing the :: moves the _usage _of environ into stlplus namespace, which stops the compilation error, but breaks the code. The declaration of environ should be moved to global namespace and the :: on line 498 restored.

    I have moved the declaration of environ: to line 26:

    diff -r1.18 -r1.19
    > extern char** environ;
    <   extern char** environ;

  • Charles McGarvey

    Then fix it how you see best.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks