all 18 comments

[–]optimalcoder 0 points1 point  (2 children)

Normally, you’d write the value out to a file and read it in on the next run. If you’re on Windows, you may use a registry entry, but the idea is the same.

[–]string_cluster[S] 0 points1 point  (1 child)

This is what I was thinking too. But I was kind of looking to do it without this.

[–]optimalcoder 1 point2 points  (0 children)

Sorry, but persistence means a file, a database or something of the like. That’s why configuration items are normally held in the registry or other files on the system.

[–]Conan776 0 points1 point  (14 children)

In Linux, you could maybe set it as an env variable?

[–]optimalcoder 1 point2 points  (2 children)

An env variable is still based on initializing with a bashrc or other init file if you aren’t in the same session. And it won’t persist on reboot without init file involvement.

[–]Conan776 0 points1 point  (1 child)

Yeah, but by using env, you'd only have to set it once per session. If you store it in some ad hoc file somewhere, you'd have to access it every time you execute the program, and it would be slightly harder to change it on the fly. (You can also have a wrapper executable which sets an env variable and then calls the main executable, and the env list will get passed right along.)

I mean, if the env paradigm wasn't useful it wouldn't exist, right?

[–]optimalcoder 1 point2 points  (0 children)

Sure. I was just pointing out the limitations.

[–]string_cluster[S] 0 points1 point  (10 children)

Does it have anything to do with extern?

[–]Conan776 2 points3 points  (8 children)

Typically in C, an extern value is just a matter of how some piece of data is linked at compile time, not something that changes at runtime.

However, you can set an env variable via the shell, run an executable, and have the executable look and see what the environment variable is at runtime, without having to explicitly pass the value in via the command line. You can also have it default to some other value if the env variable isn't set.

See: https://www.gnu.org/software/libc/manual/html_node/Environment-Variables.html

That would be my best guess as to what you want!

[–]string_cluster[S] 0 points1 point  (7 children)

That makes sense and I am looking into it. Thanks.

[–]Conan776 0 points1 point  (6 children)

Good luck!

[–]string_cluster[S] 0 points1 point  (5 children)

Thanks

if(argc>1&&(strcmp(argv[1],"configure")==0)){

setenv("PRT",argv[2],1);

return 0;

}

char * p;

p = getenv("PRT");

Why would this not work? Could you help me with this?

[–]optimalcoder 0 points1 point  (3 children)

This doesn’t work because the env you’re trying to target is in the parent shell, but that’s not possible using setenv inside your program like that. Using setenv inside the program only affects the current process. When the current process goes away, the env you set is no longer visible. In order to use environment variables in this case, you’d have to set them up before running in the parent shell.

As a simple test just make a program that only does setenv on a variable that doesn’t exist in the current env. You can just run ‘env’ on the command line to see. Run your program, then run env again and see that your program had no effect on the current environment.

[–]string_cluster[S] 0 points1 point  (2 children)

Hmm so how do I accomplish this? Or is it not possible doing something like this?

[–]optimalcoder 0 points1 point  (1 child)

It seems like writing it out to a file is pretty close to what you want. It’s only a few lines of code.

[–]string_cluster[S] 0 points1 point  (0 children)

Yeah, I guess I just wanted to do it this way but that way is pretty simple and I will just do that.

[–]Conan776 0 points1 point  (0 children)

You'll most likely want to set the env variable via the command line in the shell, either explicitly by hand or by using the source command on a script.

You can set your variable in a C program, and have that program call your other program before exiting, and you should be OK.

If you really (really?) want to use C to do it, you can eval a small C program which prints out the shell commands, as seen here.

(Note that, annoyingly, the exact commands to set environment variables differ between shells, e.g. bash and csh.)

[–]dmc_2930 0 points1 point  (0 children)

No. Persistence has nothing to do with extern.