【发布时间】:2014-06-10 09:55:47
【问题描述】:
我需要为 C 项目使用环境变量,所以我在终端中执行此操作:
export FILE_CONFIG="/home/pc/file.conf"
file.conf 是我创建的文件。
如果我在终端中执行env,我可以在列表中看到“FILE_CONFIG”及其值(/home/pc/file.conf)。
我想分配给 path_to_config -> /home/pc/file.conf
所以在 .C 程序中我这样做了:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char* path_to_config = getenv("FILE_CONFIG");
但是getenv 没有返回 FILE_CONFIG 的路径。
当我查看调试模式时 path_to_config 值为 0x0。
我已经尝试过使用其他环境变量,但我无法使用我导出的这个环境变量。
【问题讨论】:
-
只是为了测试,在你的
getenv之前做一个system("env")。 -
只是为了确保:设置变量的 shell 必须与调用二进制文件的 shell 相同。是这样吗?
-
嗯,它打印了一个列表,但我看不到我的环境变量
-
Marcus Rickert,我不知道是否将变量设置在同一个shell中
标签: c linux path environment-variables getenv