Issue with math.h - c

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.

Related

Why I can't reference the implement of function defined in .h file? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed last month.
IDE: VS code
gcc version: 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
I new in C , now I have three files:
Get_para.h
#ifndef _GETPARA_H_
#define _GETPARA_H_
extern double C;
extern double dEarthAngularVelocity;
...
extern void calParameter();
#endif
and Get_para.c, which is the implement of Get_para.h
#include <math.h>
#define _USE_MATH_DEFINES
#define pi M_PI
double C = 3e8;
double dEarthAngularVelocity = 7.29210e-5;
...
void calParameter(){
...
}
then, I want to include Get_para.h in test.c and call calParameter function which is implemented in Get_para.c
#include <stdio.h>
#include "Get_para.h"
int main(){
calParameter();
printf("%lf\n",dSemiMajorAxis);
}
I use 'run code' in VS,the command in terminal is:
if ($?) { gcc test.c -o test } ; if ($?) { .\test }
the output is:
C:\Users\bob\AppData\Local\Temp\ccuWLUIl.o:test.c:(.text+0xe): undefined reference to `calParameter'
C:\Users\bob\AppData\Local\Temp\ccuWLUIl.o:test.c:(.rdata$.refptr.dSemiMajorAxis[.refptr.dSemiMajorAxis]+0x0): undefined reference to `dSemiMajorAxis'
collect2.exe: error: ld returned 1 exit status
but I want to just include the "Get_para.h" then I can use the implement of them in "Get_para.c".I search this in google , others' code didn't work on my computer. Now I guess the problem is the parameters of gcc, but can't figure out what is it or what knowledge of C I need to know to solve this problem.
You have to compile the implementation of the function too:
gcc Get_para.c test.c -o test
Consider learning a build system, like cmake.
#define _USE_MATH_DEFINES
For a macro like this to work, it has to be defined before any includes.
#define _USE_MATH_DEFINES
#include <math.h>
#define _GETPARA_H_
It's not valid to define your macros with leading _ followed by an upper case letter. Such identifiers are reserved. For example, use:
#define GETPARA_H_

Where did sincos go? (gcc 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.

C header file is causing warning "ISO C requires a translation unit to contain at least one declaration"

Using Qt Creator I made these plain C files just to test my understanding:
main.c
#include <stdio.h>
#include "linked.h"
int main()
{
printf("Hello World!\n");
printf("%d", linked());
return 0;
}
linked.h
#ifndef LINKED_H_
#define LINKED_H_
int linked(void);
#endif // LINKED_H
linked.c
int linked()
{
return 5;
}
The IDE shows a warning on the line of linked.h in-between #define LINKED_H_ and int linked(void); which reads
ISO C requires a translation unit to contain at least one declaration
My best guess about what this means is that any header or other C file, if it is in a project, should get used in the main file at least once somewhere. I've tried searching the warning but if this has been answered elsewhere, I'm not able to understand the answer. It seems to me I've used the linked function and so it shouldn't give me this warning. Can anyone explain what's going on?
The program compiles and runs exactly as expected.
I think the issue is that you don't #include "linked.h" from linked.c. The current linked.c file doesn't have any declarations; it only has one function definition.
To fix this, add this line to linked.c:
#include "linked.h"
I don't know why it says this is an issue with linked.h, but it seems to be quite a coincidence that the line number you pointed out just happens to be the line number of the end of linked.c.
Of course, that may be all this is; a coincidence. So, if that doesn't work, try putting some sort of external declaration in this file. The easiest way to do that is to include a standard header, such as stdio.h. I would still advise you to #include "linked.h" from inside linked.c, though.
add a header
#ifndef LINKED_H_
#define LINKED_H_
#include <stdio.h>
int linked(void);
#endif // LINKED_H
The way you wrote the code, you need to use:
extern int linked(void);
(notice the additional "extern"). That might help with the issue.
Also, the code in linked.c should be:
int linked(void)
{
return 5;
}
(Notice the "parameter" - "void").
According to IBM, you need some declaration in the header file, but you do have one. Perhaps LINKED_H_ is defined elsewhere, or the compiler is seeing that it's possible that the precompiler condition might result in an empty parse.
Perhaps this header file will work for you:
linked.h
#ifndef LINKED_H_
#define LINKED_H_
int linked(void);
#endif // LINKED_H
char __allowLinkedHToBeIsoCCompliant = 1;

Keil µvision 5 header file shows an error, however it is compiled without problems

I got access to this project. It is compiled in Keil µvision 5. When I compile the project it has no errors. However, when I access a header file it shows me an error, saying that s8 variable has the following error = error: unknown type name 's8'.
typedef struct
{
s8 str[PARAM_TEXT_SIZE];
}
text_struct;
The variable is defined as follow:
typedef char s8;
I wonder whether I have misconfigured the compiler, or why this error is neglected after compiling.
PS: This is my first question in the StackOverflow site. Sorry if my question is not clear or is wrongly placed.
If s8 is defined in a header, let's say foo.h, and your text_struct is defined in another header, call it bar.h then it compiles perfectly if the file that includes bar.h includes foo.h first. It is however not clean, it's usually a good practice not to rely on such pre-requisite includes.
Here's a very simple example :
foo.h
typedef int myType;
bar.h
typedef struct {
myType x;
} myStruct;
main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "foo.h"
#include "bar.h"
myStruct y;
int main(void) {
return 0;
}
This will compile without issue, however if bar.h is statically evaluated then it'll produce an error since myType is not known here. If bar.h is included without foo.h to be included as well, then you'll have a compilation error. Here's an example :
error.c
#include "bar.h"
myStruct z;
gcc -I. error.c -o error.o
In file included from error.c:1:0:
bar.h:2:5: error: unknown type name 'myType'
myType x;

GCC/C++ cannot link libraries

I am just a beginner in C++. I am trying to construct some header file header.h, but the output is always like the following:
/tmp/ccTmZKXX.o: In function `main':
main.c:(.text+0x13): undefined reference to `func'
collect2: ld returned 1 exit status
Could you please help me to see whether my way of using header file is correct or not? Thanks a lot!
Main code (main.c):
#include "stdio.h"
#include "func.h"
main() {
double a = f(2.3);
printf("a=%f\n", a);
}
where func.c contains:
double func (double x) { return x ;}
where func.h contains:
double func (double);
And I compile with:
gcc -o main main.c
There are multiple problems here:
The C++ compiler in the GCC (GNU Compiler Collection) is g++, not gcc; the latter is the GNU C Compiler.
The code in main.c is a (not very good) C program and not a C++ program. C99 outlawed the implicit int return type; C++ essentially never allowed it.
Your question uses a function f; your compilation error references func. This means you did not show us exactly the code you tried to compile.
The standards say #include <stdio.h>; you should too.
#include <stdio.h>
#include "func.h"
int main()
{
double a = func(2.3);
printf("a=%f\n", a);
}
NB: This is a perfectly good C program if you work with C99. In C89, you are expected to return a value from main() rather than 'fall off the end'. C99 follows C++98 and allows falling off the end as equivalent to an explicit return 0;. I tend to put the explicit return(0); (usually with, sometimes without, the parentheses - the compilers don't mind either way) anyway. (I compile C with -Wstrict-prototypes; to get a warning-free compilation, I write int main(void), which also works with C++ but the void is not necessary there.)
The header is OK, though you will learn in due course about header guards and other paraphernalia that make headers more reliable.
#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED
extern double func(double a);
#endif /* FUNC_H_INCLUDED */
The extern is not mandatory. I tend to use it, but there are many who do not.
The source file defining the function should include the header to ensure that the function definition is consistent with the declaration. All code that uses the function should include the header so that there is a prototype in scope. This cross-checking is crucial for reliability. C++ requires prototypes in scope before a function is used; it does not demand a prototype in scope before the function is defined (but it is good practice to do so). It is strongly recommended in C that you have a prototype in scope before defining an external (non-static) function. You can use -Wmissing-prototypes with C code and GCC to spot such problems, but the option is not valid for G++.
#include "func.h"
double func(double x) { return x; }
Since this is a C++ question, we could consider inlining the function in the header. Indeed, C99 also supports inline functions. However, we can ignore that for the time being.
Since this is a C++ question, we could consider that using <stdio.h> is not good because it is not type safe. You might be better off using <iostream> et al, not least because they are type safe.
#include <iostream>
#include "func.h"
int main()
{
double a = func(2.3);
std::cout << "a=" << a << std::endl;
}
The correct compilation requires both the main program and the function it invokes, so you might write:
g++ -o main main.c func.c
Or, if you are compiling it in C, then:
gcc -std=c99 -o main main.c func.c
Note that the -std=c99 is necessary to ensure that the absence of return in main() is acceptable.
Note that there are several extensions in use for C++ source code, including .C, .cpp and .cxx, all of which are accepted by G++ (as well as .c).
There are several things wrong here.
Define the function as follows in func.h
extern double func(double);
When compiling, provide all source (c, cpp) files
gcc main.c func.c -o main
You should be good to go.
Compile like this:
gcc -o main main.c func.c
Then it will be fine.

Resources