Stdlib.h has no declaration for putenv

I tried compiling the following code with gcc 4.7.3 and clang 3.2.1 on Ubuntu 13.04 (64-bit):

 #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() { putenv("SDL_VIDEO_CENTERED=1"); return 0; } 

I expected putenv be declared in the stdlib.h header, but I get the following warning:

 test.c: In function 'main': test.c:6:5: warning: implicit declaration of function 'putenv' [-Wimplicit-function-declaration] 

Why is the declaration for this function not in my header?

+6
source share
1 answer

You need to define specific macros. Take a look at man 3 putenv :

 NAME putenv - change or add an environment variable SYNOPSIS #include <stdlib.h> int putenv(char *string); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): putenv(): _SVID_SOURCE || _XOPEN_SOURCE 

Try defining either _SVID_SOURCE or _XOPEN_SOURCE before including stdlib.h , for example:

 #define _XOPEN_SOURCE #include <stdlib.h> 

Or when compiling (with -D ), for example:

 gcc -o output file.c -D_XOPEN_SOURCE 
+8
source

Source: https://habr.com/ru/post/946285/


All Articles