error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token in C - c

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.

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.

How to use a function from an object file

I'm pretty sure this question is a duplicate, but can't find an answer.
I wrote a function (in "function.h" and "function.c" files), and compiled it to "function.o" file. I want to use the function defined in "function.c" in my main source, but without including "function.h". Is that possible, to compile main.c using just "function.o"?
A header file is (usually) just a list of declarations which are inserted textually (by #include) into your source files.
Therefore, if function.h contains
void foo(int x);
and you have #include "function.h" in your main source file, it is exactly equivalent to just writing void foo(int x); in your source file.
Header files are useful for code organization because it would be highly inefficient (and error-prone) to copy those declarations by hand into every source file that used them. But, if you want to avoid the header file for any reason, copying those declarations directly into your source file has the same effect as #include'ing the file.

Why is initializing this way in C giving multiple definition error?

Below are two files which I will use as my example. If I define an array of structures in file1.h and have file2.c include file1.h I would get a multiple definition error. Why is that? If I just have struct thread tasks[32] I don't get this error.
file1.h
...
...
struct thread tasks[32] = {0}; // thread is structure defined above
...
...
file2.c
#include file1.h
Most likely you are including the header file in more than one source file. The #include directive literally includes the contents of the header file into the source file, which means that all code in the header file will also be in the source file. This means that if two or more source file includes the same header file then the code in the header file will be duplicated.
The = {0} turns the line from a declaration into a definition. You can have as many (compatible) declarations of a file-scope variable as you like, but at most one definition; by including the header in multiple source files you are causing multiple definitions to be generated.
You can prevent problems from multiple includes by wrapping the contents of your header files in #ifndef like this
/* file1.h */
#ifndef INCLUDE_FILE1
#define INCLUDE_FILE1
/* contents here */
#endif

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 *

Headers and multiple sources

Hey. I have this in a header file:
struct something {
int a;
int b;
};
int all[25][9];
This header file is included in all 3 .c files that I have on my project. One of the files (the main file) has the main function and the others have functions that are used in the main file. They also use variables that are declared on this main file, by using extern type variableName. However, while I do declare struct something *stuff; and later malloc it on the main file (and these other files work with this stuff directly), my all 2d array isn't declared anywhere but the header file. I use this array in one of those extra .c files. Will this all array be declared in each of them? Should I do it this way? It's imperative that there's a reference to all in that header file, for my purposes. Should I just declare all as all[][] and then assign a size to it on the .c file, or something like that?
If you want multiple source files to share a single array called all you should declare
extern int all[25][9];
in the header and
int all[25][9];
in one of the c files.
Use extern keyword to declare the array in your header:
extern int all[25][9];
Then instantiate it in just one of the implementation files:
int all[25][9];
Other C files include the header and can access the array.
You should not do it this way. This way creates a definition of all in every source file that includes the header, and multiple definitions of the same object are not allowed (in pratice, you might get a seperate instance of all in each source file, or they might all refer to the same one).
Instead, in the header file, put only declarations:
extern int all[25][9];
Then in one C file (probably your "main" file you mention), put the definition:
int all[25][9];
In the header file define/declare as
EXT int a;
In the main c file use
#
define EXT extern
#include <a.h>
#undef EXT
This will avoid seperate definition /declaration

Resources