我需要获取ANDROID_HOMEOSX上环境变量的值(在.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");
}
Run Code Online (Sandbox Code Playgroud)
输出始终为The ANDROID_HOME is NOT set.。我认为我在getenv()这里使用不正确。要么,否则.bash_profile在getenv()调用时无效。
我想念什么?
您的代码似乎正确-因此,您很可能在确实未设置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");
}
Run Code Online (Sandbox Code Playgroud)
用以下命令编译:
g++ getenv.cpp -o getenv
Run Code Online (Sandbox Code Playgroud)
现在运行:
./getenv
The ANDROID_HOME is NOT set.
export ANDROID_HOME=something
./getenv
The ANDROID_HOME is: something
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1479 次 |
| 最近记录: |