关于OSX上的getenv()
About getenv() on OSX
我需要获得OSX上环境变量ANDROID_HOME
的值(在.bash_profile中设置)。我可以通过在终端输入echo $ANDROID_HOME
来验证它的存在。
代码如下:(Xcode项目)
void testGetEnv(const string envName) {
char* pEnv;
pEnv = getenv(envName.c_str());
if (pEnv!=NULL) {
cout<< "The " << envName << " is: " << pEnv << endl;
} else {
cout<< "The " << envName << " is NOT set."<< endl;
}
}
int main() {
testGetEnv("ANDROID_HOME");
}
输出始终为The ANDROID_HOME is NOT set.
。我认为我在这里没有正确地使用getenv()
。要么这样,要么。当调用getenv()
时,bash_profile不生效。
我错过了什么?
你的代码看起来是正确的-所以你很可能在ANDROID_HOME确实没有设置的环境中调用你的程序。你是如何开始你的项目的?
我改变了你的源代码,实际上是可编译的,它在我的OS X系统上工作得很好:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void testGetEnv(const string envName) {
char* pEnv;
pEnv = getenv(envName.c_str());
if (pEnv!=NULL) {
cout<< "The " << envName << " is: " << pEnv << endl;
} else {
cout<< "The " << envName << " is NOT set."<< endl;
}
}
int main() {
testGetEnv("ANDROID_HOME");
}
用:
g++ getenv.cpp -o getenv
现在运行:
./getenv
The ANDROID_HOME is NOT set.
export ANDROID_HOME=something
./getenv
The ANDROID_HOME is: something
相关文章:
- Linux的Cpp上的计时器
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 物理键盘上的触发器按键
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 集合上的输出迭代器:assign和increment迭代器
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 对于set上的循环-获取next元素迭代器
- 如何从棋盘上的箱号中找到行和列
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- if数组上的随机数
- 向量上的线性搜索
- 位阵列上的快速AND运算
- 无法将 GLFW 库与 Ubuntu 18.04 上的头文件链接
- 为什么一个向量上的多线程操作很慢
- 64位机器上的C++内存对齐
- 关于OSX上的getenv()