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?
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
Related
I am using MSYS2 mingw 64 when compiling code that needs the header random.h I am trying to make that code work on both Linux and windows with the least amount of changes
#include <sys/random.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
srand(time(NULL));
return 0;
}
I ran this command pacman -S msys2-runtime-devel to download the random.h header file and it is located in sys official link
on linux, the file is included using #include <linux/random.c> but I don't know what to use on windows or if I have to do something completely different
When I comment the first line I get this warning
main.c:10:9: warning: implicit declaration of function 'srand' [-Wimplicit-function-declaration]
10 | srand(time(NULL));
| ^~~~~
As per the linked documentation,
srand is declared in #include <stdlib.h>.
rand is declared in #include <stdlib.h>.
Neither requires including random.h or linux/random.c.
I don't understand why the function getpagesize gives me a warning for implicit declaration of function while using the c18 version of gcc.
gcc test.c -Wall -std=c18
implicit declaration of function ‘getpagesize’ [-Wimplicit-function-declaration]
nested extern declaration of ‘getpagesize’ [-Wnested-externs]
int BLOCKSIZE = getpagesize();
And this is my included files :
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdint.h>
#include <errno.h>
Using -std=cXX instead of -std=gnuXX disables a bunch of normally defined feature test macros, including the ones that provide getpagesize(). From its man page (Assuming you're using linux):
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getpagesize():
Since glibc 2.19:
_DEFAULT_SOURCE || ! (_POSIX_C_SOURCE >= 200112L)
From glibc 2.12 to 2.19:
_BSD_SOURCE || ! (_POSIX_C_SOURCE >= 200112L)
Before glibc 2.12:
_BSD_SOURCE || _XOPEN_SOURCE >= 500
So you have to define the appropriate one to the appropriate value before including any header files. Or just use -std=gnu18.
Edit: Also, since getpagesize() is obsolete and not standard, consider using the POSIX standard sysconf(_SC_PAGESIZE) instead.
This question already has answers here:
popen implicitly declared even though #include <stdio.h> is added
(3 answers)
Closed 4 years ago.
I want to use popen. It is in stdio.h. I include that, but the compiler doesn't see it with
-std=c11. It does compile without -std=c11.
#include <stdio.h>
int main(void)
{
popen("ls *","r");
}
gcc -std=c11 popen_test.c
popen_test.c: In function ‘main’:
popen_test.c:5:4: warning: implicit declaration of function ‘popen’ [-Wimplicit-function-declaration]
popen("ls *","r");
^~~~~
It is hidden in stdio.h with
#ifdef __USE_POSIX2
The man page says it is available if:
_POSIX_C_SOURCE >= 2 || /* Glibc versions <= 2.19: */
_BSD_SOURCE || _SVID_SOURCE
popen is not part of C. To get it, you need to enable it with a feature test macro before including anything.
The simplest way to do it is with a #define _GNU_SOURCE at the top (or with -D_GNU_SOURCE in your compiler invocation).
compiles with -std=c11:
#define _GNU_SOURCE
#include <stdio.h>
int main(void)
{
popen("ls *","r");
}
I'm getting this warning: (-std=c99 -pedantic)
warning: implicit declaration of function ‘strndup’ [-Wimplicit-function-declaration]
but I'm importing these libs:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
So what?! :(
// file.c:
#include "file.h"
strndup(...)
// file.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
The issue is your usage of the -std=c99 option. Since strndup() isn't part of C99, and you're asking the compiler to go into standards compliant mode, it won't provide the prototype for it. It still links of course, because your C library has it.
While you may be able to coax gcc into providing it by specifying feature macros yourself, I'd say it doesn't make much sense to be in C99 compliance mode and ask for GNU extensions for example. gcc already provides a mode for this, which will solve your warning: -std=gnu99.
My man strndup says
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
strdup():
_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
|| /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
strndup():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
Before glibc 2.10:
_GNU_SOURCE
strdupa(), strndupa(): _GNU_SOURCE
So I'd need to, eg, #define _POSIX_C_SOURCE 200809L before the first #include in your file.
see man 7 feature_test_macros
strndup is a GNU extension, so you need to compile with -D_GNU_SOURCE on the command line, or stick a #define _GNU_SOURCE 1 in your source files before the #include lines
This happened to me, and I added #define _XOPEN_SOURCE 500 and the warning went away.
Consider the following C code:
#include <stdio.h>
#include <stdlib.h>
void fatal(const char* message){
/*
Prints a message and terminates the program.
Closes all open i/o streams before exiting.
*/
printf("%s\n", message);
fcloseall();
exit(EXIT_FAILURE);
}
I'm using clang 2.8 to compile: clang -Wall -std=gnu99 -o <executable> <source.c>
And get: implicit declaration of function 'fcloseall' is invalid in C99
Which is true, but i'm explicitly compiling to gnu99 [which should support fcloseall()], and not to c99.
Although the code runs, I don't like to have unresolved warnings when compiling.
How can i solve this?
Edit: corrected tipo.
To include non-standard extensions when you include standard headers you need to define the appropriate feature test macro. In this case _GNU_SOURCE should work.
#define _GNU_SOURCE
#include <stdio.h>
This is independent of -std=gnu99 which enables language extensions, not library extensions.
Here in the man page of fcloseall()
#define _GNU_SOURCE
#include <stdio.h>
You have to define macros _GNU_SOURCE is you snippet, along with stdio.h header. _GNU_SOURCE is a feature test macros which is used to create portable application.