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 *
Related
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"
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
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
I have the following declarations
FILE *fptr;
FILE *optr;
in algo.h
I have the main in main.c which opens these files.
I get the above error if I put the declarations in the header file. If I put it in the main.c, then I get mutiple definition errors like
src\main.o:main.c:(.bss+0xc88): multiple definition of rcount'
src\new_algo.o:new_algo.c:(.bss+0xc88): first defined here
src\main.o:main.c:(.bss+0xc8c): multiple definition ofcondi'
src\new_algo.o:new_algo.c:(.bss+0xc8c): first defined here
Kinda sounds like you (1) haven't included <stdio.h> where you're using FILE, and/or (2) have some non-static executable code or non-extern variable definitions in your headers (and/or are #includeing a C file).
The first would typically cause FILE not to be defined (or to be typedef'd to a type that doesn't exist, in some cases). The second would cause stuff to be defined in each translation unit that includes the file, which would confuse the linker.
To fix: (1) #include <stdio.h> in the file where FILE is used, and (2) move shared definitions from the headers into a .c file (and/or declare them as static or extern as appropriate), and only ever #include .h files.
What you have in algo.h is a definition not a declaration. If you have FILE *fptr; FILE *optr; in both the source and the header file then you are declaring the variables twice.
You need:
algo.h
extern FILE *fptr;
extern FILE *optr;
algo.c
FILE *fptr;
FILE *optr;
Sounds like you're not including stdio. Add
#include <stdio.h>
in your header file above those declarations.
The linker errors have nothing to do with the FILE variables you posted.
There are two variables your source, named rcount and condi, that according to the linker is defined in both your source files. The reason is, I guess, that you define those variables in a header file that is included in both source files. Some old compilers still can't handle that.
This is my warning.
warning : implicit declaration of function 'sqlglm'
The warning comes in a bi.pc file.
when i check the bi.c file.
it doesn't include
#include <sqlcpr.h>
#include <sqlca.h>
As .c file generate at compile time.
there is no need to edit .c file
i am using linux & gcc compiler,C.
This is an old question, but to be helpful to people who might stumble on it via search engine like me, the right answer is:
you have to add the following lines to your Pro*C source
EXEC SQL INCLUDE sqlca.h;
EXEC SQL INCLUDE oraca.h;
EXEC SQL INCLUDE sqlcpr.h;
The sqlca.h and oraca.h are required before sqlcpr.h can be included. A standard C header like stddef.h or stdio.h have to be included before these embedded SQL statements as they need to have size_t defined.
It is important to use the embedded SQL include statements instead of the C #include.
The EXEC SQL INCLUDE will copy the content of the header file into the generated C file. Therefore, it is not necessary to add the other include file paths of the Oracle client into the C compiler command line.
warning : implicit declaration of function 'sqlglm' shows up when function has been defined in some other header file, but has not been #included, or the function has not been defined at all. So, include the file which defines it or define it yourself.
Update
Use #include "sqlcpr.h" (in case sqlcpr.h is not in compiler's search paths and is in the same directory as the source file)