Where did sincos go? (gcc c) - c

Couple days ago it was working fine, but trying to use again today, my code editor cannot find sincos anymore and GCC throws me a warning that it cannot find sincos when compiling.
Here's the code:
// file: main.c
#include <math.h>
int main() {
double sin, cos;
sincos(0.0, &sin, &cos);
return 0;
}
Using gcc:
$ gcc main.c -lm
x.c: In function ‘main’:
x.c:5:2: warning: implicit declaration of function ‘sincos’ [-Wimplicit-function-declaration]
5 | sincos(0.0, &sin, &cos);
| ^~~~~~
x.c:5:2: warning: incompatible implicit declaration of built-in function ‘sincos’
x.c:2:1: note: include ‘<math.h>’ or provide a declaration of ‘sincos’
1 | #include <math.h>
+++ |+#include <math.h>
2 |
It says I should include math.h yet I do.
It says it can't find sincos yet it compiles and runs fine. I'm just annoyed by those warnings. Anyone knows what's wrong?

Add the following to the top of the file to enable the gnu extension:
#define _GNU_SOURCE
#include <math.h>
This will prevent the warnings. Note that this is a glibc extension and not part of the C standard.

Related

Where is random.h in msys2

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.

Issue with math.h

I am getting a really weird issue with my code.
I tried using clang and gcc, both tell me the same thing
init_twiddle.c:12:10: warning: implicitly declaring library function 'cosf' with type 'float (float)' [-Wimplicit-function-declaration]
.re = cosf(primitive_root*i) ,
^
init_twiddle.c:12:10: note: include the header <math.h> or explicitly provide a declaration for 'cosf'
init_twiddle.c:13:10: warning: implicitly declaring library function 'sinf' with type 'float (float)' [-Wimplicit-function-declaration]
.im = sinf(primitive_root*i)
^
init_twiddle.c:13:10: note: include the header <math.h> or explicitly provide a declaration for 'sinf'
2 warnings generated.
The code:
// file: init_twiddle.c
#include "complex.h"
#include <math.h>
void init_twiddle1024(Complex__complex* twiddle) {
int i,span ;
// Init the twiddles
for(span=1;span<=512;span<<=1) {
float primitive_root = -Complex__pi/span ;
for(i=0;i<span;i++) {
Complex__complex t =
{
.re = cosf(primitive_root*i) ,
.im = sinf(primitive_root*i)
} ;
twiddle[span+i] = t ;
}
}
}
// file: complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
#include "stdbool.h"
#include "assert.h"
//#include "pervasives.h"
typedef struct Complex__complex {
float re;
float im;
} Complex__complex;
static const float Complex__pi = 3.141593;
#endif // COMPLEX_H
The command I use to compile:
gcc -I. -I$(heptc -where)/c/ -std=c99 -c init_twiddle.c
I am working on a project with some strange programming language which explain all the included directories.
Does someone have any idea of why I am getting those errors?
PS: note that it's not a linker issue but an issue at compile time.
It also does not seem to appear when I manually write the content of complex.h into the file
It turned out Barmar was right. I was including a directory where a math.h already exists, thus leading to not including the libc one.
The faulty one was -I$(heptc -where)/c/ for those who would have the same issue with the Heptagon langage.
Thanks for your help.
As commented by #Barmar, the issue is that in $(heptc -where)/c/ there is already a math.h header defined which don't implement the function you want to use in this exemple.
Considering it's to compile with Heptagon I would advise to copy the only file that is really useful in this case from $(heptc -where)/c/ which is pervasives.h where you have your init_twiddle.c and since you are compiling with -I. it will then compile perfectly fine.

Why does -std=c11 with gcc hide popen in stdio.h? [duplicate]

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");
}

stdlib.h doesn't have declaration for putenv

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

getting "implicit declaration of function 'fcloseall' is invalid in C99" when compiling to gnu99

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.

Resources