今天在使用getenv获取环境变量的时候遇到这个问题,代码类似于这样

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h> 

extern "C" {
   char* getenv(const char*);
}

int main(void) {
  char *value;
  value = getenv("HOME");
  printf("%s\n", value);
}

编译的时候会出现一个warning

1
2
test.c: In function `main': 
test.c:9: warning: assignment makes pointer from integer without a cast

运行的时候会出现segment fault

原来是缺少stdlib.h的问题(可是我已经申明了原型呀……)

加入stdlib.h解决问题:

1
#include <stdlib.h>