Search code examples
c++visual-c++boostvisual-studio-2015boost-process

Visual Studio 2015: Can't find `char * * __cdecl __p__environ(void)`


I'm trying to build boost.process with VS2015 WIN64 (did it some time ago with VS2010 WIN32 and WIN64 without any problem).

The linker complains about char * * __cdecl __p__environ(void) not being found:

process.obj : error LNK2019: symbole externe non résolu "char * * __cdecl __p__environ(void)" (?__p__environ@@YAPEAPEADXZ) référencé dans la fonction "public: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > __cdecl boost::process::self::get_environment(void)" (?get_environment@self@process@boost@@SA?AV?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@std@@@2@@std@@XZ)

Here is the code leading to this link problem:

#if defined(BOOST_WINDOWS_API) 
#ifdef GetEnvironmentStrings 
#undef GetEnvironmentStrings 
#endif 
    char *environ = ::GetEnvironmentStrings();
    if (!environ) 
        boost::throw_exception(boost::system::system_error(boost::system::error_code(::GetLastError(), boost::system::get_system_category()), "boost::process::self::get_environment: GetEnvironmentStrings failed")); 
    try 
    { 
        char *env = environ; 
        while (*env) 
        { 
            std::string s = env; 
            std::string::size_type pos = s.find('='); 
            e.insert(boost::process::environment::value_type(s.substr(0, pos), s.substr(pos + 1))); 
            env += s.size() + 1; 
        } 
    } 
    catch (...) 
    { 
        ::FreeEnvironmentStringsA(environ); 
        throw; 
    } 
    ::FreeEnvironmentStringsA(environ); 
#endif 

Replacing char *environ = ::GetEnvironmentStrings(); by char *environ = NULL; does not fix the issue. Commenting the whole bloc fixes the issue.


Solution

  • I'm so unlucky....

    stdlib.h has #define environ _environ (probably new in VS2015) ...

    Adding #undef environ or replacing environ by environVar in boost.process code (or anything you like) fixes the compilation....

    Note: Not closing the OP because someone else trying to compile boost.process may hit the same issue.