clinuxpathenvironment-variablesgetenv

Linux Environment Variable and C project getenv()


I need to use an environment variable for a C project so i did this in a terminal:

export FILE_CONFIG="/home/pc/file.conf"

file.conf is a file which i created.

If I do env in the terminal, I can see "FILE_CONFIG" in the list with its value (/home/pc/file.conf). I want to assign to path_to_config -> /home/pc/file.conf SO in a .C program i did this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

    char* path_to_config = getenv("FILE_CONFIG");

But the getenv doesn't returns the path to FILE_CONFIG.. When I look in Debug mode path_to_config value is 0x0. I've tried with other environment variables but I could not do with this one in particular which I exported.


Solution

  • Let me guess: you are running your program from the IDE. The environment an IDE provides to your program is totally unrelated to the environment where you export your variable. Suggestion: run your program from the command line at the terminal in which you did export. You shall see your variable all right.

    Then search your IDE for a way to specify an environment for a target program, and set it there.

    Optionally, add the export line to your shell's startup script.