名前がstdinによって与えられるすべての環境変数を出力するCプログラムがあります。$PATH、$USER などの変数を出力しますが、Linux シェルで自分で定義した環境変数は表示されません...たとえば、~.bashrc で MYTEST=test_is_working をエクスポートしてから、bashrc をソースしました (source ~/.bashrc)。プログラムが getenv で test_is_working を返すことを期待していましたが、そうではありません。
#include <QCoreApplication>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
char* my_env= getenv("MYTEST");
if(my_env!=NULL){
printf("my env is : %s \n", my_env);
}
else {
printf("can't find env \n");
}
return a.exec();
}
それが返されます:envが見つかりません
一方、ターミナルを開いて「env」と入力すると、MYTEST=test_is_working となります。
同様の投稿を見ました: Using getenv function where the solution is to launch the program from the shell. しかし、Qtcreator で実行およびデバッグしているため、できません。
どこが間違っているのかわからないのですが、どなたか教えていただけないでしょうか?
ありがとう