How to fix " implicit declaration of function" - c

I'm working on a GUI library in C for a school work, but I've got a problem with header files (I think).
I have a function "rectangle_coordinates" in a "draw.c" file, with the adequate "draw.h" header file, and in a file "widget_frame.c" I call this function, after including the "draw.h" header file.
But I still got a warning :
./src/ei_widget_frame.c:40:5: warning: implicit declaration of function 'rectangle_coordinates' [-Wimplicit-function-declaration]
rectangle_coordinates(&frame_to_draw, &xmin, &xmax, &ymin, &ymax);
Do you have an idea that can fix my problem ?

At a minimum, all things below should be verified and corrected (if needed).
The function name must be the same in all places (check for typo's).
The return type must be the same in all places.
The parameters list must be the same in all places.
In draw.h you should have something like:
extern /type/ rectangle_coordinates(&frame_to_draw, &xmin, &xmax, &ymin, &ymax);
where:
/type/ is the return type of the function.
the parameters list needs to be written correctly (I just made copy / paste from the warning message provided).
In both draw.c and ei_widget_frame.c you should have:
#include "draw.h"

Related

Multiple includes of the same source files through a project is correct?

I'm trying to call a method in my main, which is declared in another file.
When I try to call it with this line of code in the Main.c:
#include "SPI3.c"
void main(void) {
initSpi();
}
it gives the following error:
SPI3.c:196:: error: (237) function "_initSpi" redefined
The function is declared in the file SPI3.c
void initSpi()
{
//CODE
}
I've researched thoroughly my code and there is no redefinition of the function, and searching through the web I've seen that the error also appears when you call a function that is not declared yet or when
you include the same file more than once and it redefines the function.
I'm certain it's the former because I actually do more than one include of this file in the project, because I also need to call those methods in other files.
What am I doing wrong? There can only be one include of a source file in the whole project? Or is there another solution? Could it be that the function is just not initialized?
Thanks, feel free to ask for more details.
By including any file, you paste its contents into your file. So, the function initSpi() is defined twice: within SPI3.c and Main.c => you get redefinition. You need to include only .h headers not .c files. These header files contain only declarations (opposed to definitions), e.g.:
/* SPI3.h */
void initSpi();
So, we include header files into our file and get declarations of functions and variables that are defined elsewhere. In order to link their definitions, we can possibly need also a Makefile or a C project file.
You should not include .c files, but .h files (except if you know exactly what you are doing).
I would rather do the following thing:
in your SPI3.h file, declare your function:
void initSpi(void);
Don't forget Include guard in your .h file
and in your main.c file:
#include "SPI3.h"
Thus your function is only defined once (in your SPI3.c file), and you will not get the redefined error.
Just for clarification
When you write the following code in your .c file:
void initSpi()
{
//CODE
}
Then you both declare and define your function.
When you write the following code in your .h file:
void initSpi(void);
Then you just declare your function. It is your function prototype.
Your function can be declared multiple times, but can only be defined once (except if using the weak keyword).
That why it is recommended to declare (and only declare) your functions in .h files and to only include those files into your .c files.

Including source files in C files

I am very new to C, so I apologize for this newby question.
I would like to use this source code in my project: http://base64.sourceforge.net/b64.c.
So, I include it in my test file:
#include <stdio.h>
#include "b64.c"
int main()
{
return 0;
}
However, main() is defined in b64.c as well, so upon compiling, I get:
test.c:4:5: error: redefinition of ‘main’
b64.c:495:5: note: previous definition of ‘main’ was here
test.c: In function ‘main’:
test.c:5:1: error: number of arguments doesn’t match prototype
b64.c:495:5: error: prototype declaration
What is the correct usage of this source file, or any? How do we correctly use it, or use functions defined in that file?
Edit: I understand that the problem is due to the duplicate definitions of main. I know there can only be one. My question is rather, won't every meaningful project need it's main method? Then why is there a main method defined in b64.c? Are we just supposed to delete this method from the source code? It seems odd that the source code doesn't just come ready to be included and used.
It is never a good idea to #include a C source file into your code. You can either copy the code from the other C source file into your code, or include the needed prototypes in your code and make a call to the functions, linking those after compiling them separately.
you should use one of the two main functions.
If you want a new main, write your main method in your file and remove it from the 'b64.c' file, if you want to use it from the 'b64.c' file remove your (empty) main.
If main is defined in b64.c either you cannot simply redefine it in your main source file.
What do you want is to use several functions from b64.c in your program. Delete the main function from it and create a header file where you protoype all of the functions in b64.c. After that, include this header file in your main program. Have a look at this short Wikipedia entry. It should give you an overview of the topic.
Besides this: It seems that you aren't very familar with C. Try out some tutorials and go on reading about C.
First of all, you must redeclare the .c file as a .h, and then you have to go through the source code and rename any duplicate methods or global variable names. The main method is the starting point of a program so there can only be one.
(Usualy you dont include .c files, only .h)
if you want the functions inside the "b64.c" you should delete the main function from "b54.c"!
A C application needs one main() function. Your file b64.c looks like a self-sufficient C program so you don't need your test.c. Just compile and run b64.c.
When you are including that source file you are getting 2 main() declaration which is incorrect. So you have redefined "main" in this case.
Including .c into another .c file doesn't make sense. C files compile to .obj files, which are linked by the linker into the executable code , so there is no need to include one .C file into another. Instead, you can make a .h file that lists the functions and include that .h file

Implicit declaration of function 'cond_resched' that is defined as macro in included header

I'm trying to build a kernel with some patches that affect the same files and have a problem.
While building, i get an error:
arch/x86/include/asm/uaccess_64.h: In function 'copy_from_user': arch/x86/include/asm/uaccess_64.h:81:2: error: implicit declaration of function 'cond_resched' [-Werror=implicit-function-declaration]
At first i followed the code and found out that the pointed string is:
if (access_ok(VERIFY_READ, from, n))
access_ok(...) is a macro from file uaccess.h, that does include cond_resched() call.
Actual cond_resched() call is defined in linux/sched.h and is #included into uaccess.h file. Also i tried to include it into uaccess_64.h file but it doesn't help. So i'm out of ideas how it could be implicitly declared.

stdio inclusion in a header file

I'm not totally sure but this looks wrong:
I have a header file named fraction.h in which I store a fraction structure and the methods to handle it, one method is used to write a fraction in a file and in the signature of this function one argument is a FILE pointer.
fraction.h:
...
const Fraction * fraction_fwrite(const Fraction * f, FILE * file);
...
fraction.c:
#include <stdio.h>
#include "fraction.h"
...
Now when I try to compile a program that uses a fraction, I get an error,
here is what I have in my Makefile:
program_test: fraction.o program_test.o
and I include fraction.h in program_test.c of course.
but I keep getting this error :
fraction.h:34:54: error: unknown type name 'FILE'
someone could explain the different steps through which the compiler includes files ?
because <stdio.h> is in fraction.c so why does it strike this unfound-type error ?
should I include <stdio.h> in fraction.h ? which looks not really appropriate from my measly experience.
When you compile program_test, the compiler isn't looking at the other .c files, only the files you #include in the file you actually compile.
So, you either have to #include <stdio.h> in the test program, just like in fraction.c, or include it in the header file.
Even though the C standard says that the standard library files will not include each other, there is nothing saying that user defined files cannot do that. In fact it is usually much easier to use them if they do.
When the preprocessor processes a file, then it will copy the content of an included file into the file that is preprocessed. The reason why you don't get that error in fraction.c is that the content of stdio.h is included before fraction.h is included.
So the preprocessed file fragments.c looks like this:
stdio.h contents
fragment.h contents
fragment.c contents
Notice that the FILE definition will be included through stdio.h before it is referenced in fragment.h.
You should include stdio.h in fraction.h to get rid of this error.
Include stdio.h in program_test.c. If you dont include stdio.h in program_test.c (which includes fraction.h), the compiler doesn't know the definition of FILE used by fraction.h, and generates error.
And, you really should include stdio.h in fraction.h

Is <stdio.h> include needed for FILE in C?

I have a header file (sample.h) for my c file (sample.c). When I prototyped a function in my header file as below.
return_type sample_fun (FILE *filePtr);
I get a compilation error saying, Syntax error: possible missing ')' or ','? When I include the stdio.h error is resolved. Is the stdio.h include mandatory? Some of my files work well without the include.
I use gcc on AIX.
Yes, the type FILE is defined in stdio.h; if you mention it, then you must include that file.
Yes it is. FILE is typedefed from a struct iobuf on most platforms. This requires that the full definition of struct iobuf be present, even though all the interfaces use FILE *, and pointer types do not normally require full definitions prior to their use (C limitation).
See this question for more information: Forward declare FILE *

Resources