1

I have a C program that prints every environmental variable, whose name is given by stdin. It prints variables such as $PATH, $USER but it doesn't see the environmental variables that i defined myself in the Linux shell... For instance, in ~.bashrc I exported MYTEST=test_is_working, then I sourced the bashrc (source ~/.bashrc). I expected the program to return test_is_working with getenv but it doesn't.

#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();
}

it return : can't find env

while when I open a terminal and enter “env”, I have MYTEST=test_is_working

I saw a similar post: Using getenv function Where the solution is to launch the program from the shell. But I can't because I'm running and debugging in Qtcreator.

I don't know where I'm in the wrong, could someone explain it to me?

thanks

Community
  • 1
  • 1

1 Answers1

1
  1. Environment variable are passed only to child processes which where started after setting variable. So setting them in shell will not change anything in Qt Creator and programs started from it.
  2. Qt Creator allows to customize environment variables (I've seen it).
    Check project settings (run section) and/or Qt Creator properties (it should be easy to find).
  3. you can also set program parameters in qt creator (even redirect standard streams) it is in project settings, run section.
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • If i launch Qtcreator from the shell doesn't it make him a child process of the shell? Should i not find the variable that i already put in it? And yes i can customize environment variable in my project but it's doesn't save my variable if i give or copy my project in some other place. – user2177446 Mar 06 '14 at 11:47
  • And if add the variable directly into the code of my project each time i do a program, i lose the utility of the environment variable that are shared between my different process on my system. – user2177446 Mar 06 '14 at 11:48
  • For example i have a log system for all my program with its path has environment variable i would like to retrieve it. Maybe i can't but i would like to know if it's possible. Otherwise i will just put the path in my code. – user2177446 Mar 06 '14 at 11:49