Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stdlib.h doesn't have declaration for putenv

Tags:

c

linux

c99

I've 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 to 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 missing in my header?

like image 916
Overv Avatar asked May 31 '13 18:05

Overv


1 Answers

You have to define certain macros. 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, like so:

#define _XOPEN_SOURCE
#include <stdlib.h>

Or when compiling (with -D), like:

gcc -o output file.c -D_XOPEN_SOURCE
like image 168
Dan Fego Avatar answered Sep 28 '22 09:09

Dan Fego