#include <iostream>
namespace A {
void func();
}
void A::func()
{
extern char **environ;
std::cout << environ[0] << std::endl;
}
int main()
{
A::func();
return 0;
}
Like the code above, I just want to use the system-defined pointer **environ
in A::func()
, but g++ always says:
undefined reference to `A::environ'
How can I use the system-defined variable environ
correctly?
Add
#include <unistd.h>
and environ must be in global scope.
So the code would look like this:
#include <iostream>
#include <unistd.h>
extern char **environ;
namespace A {
void func();
}
void A::func()
{
std::cout << environ[0] << std::endl;
}
int main()
{
A::func();
return 0;
}