#include <iostream>
namespace A {
void func();
}
void A::func()
{
extern char **environ;
std::cout << environ[0] << std::endl;
}
int main()
{
A::func();
return 0;
}
像上面的代码,我只是想在A::func()
中使用系统定义的指针**environ
,但是g++总是说:
undefined reference to `A::environ'
如何正确使用系统定义的变量environ
?
最佳答案
添加
#include <unistd.h>
并且 environ 必须在全局范围内。
所以代码看起来像这样:
#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;
}
关于c++ - 如何在用户自定义命名空间的成员函数中引用指针**environ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15586869/