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
Related
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.
I have a project where I need a ton on structs, and as an example, I will use this codebyte (not from project)
typedef struct THING{
int a;
int b;
float stuff;
} THING;
The question I have is can I have a header file containing these typedefs and structs (i.e. structures.h) WITHOUT a related code file (structures.c)? Or what would be the proper method to do this?
Thanks
There's no hard rule that a header file with a given name has to have a corresponding source file of the same name.
If your header only has struct type definitions and typedefs then there's no need for there to be a corresponding .c file. If the file had function or global variable declarations then they would need to be defined in a .c file somewhere, however that file doesn't have to have the same name as the .h file.
I'm just new to this separating source files.
.
Is it possible to share the my type defined to any other source files?
.
.
.
This is my definitions in myTypeDef.h
typedef struct
{
int x, y, w, h;
short life;
}Man;
What if I create a new .h (named draw.h) file that will draw the image like this:
#include "myTypeDef.h"
void rendererFunction(Man *man);
Then I create a .c (named draw.c) file that will implement what's in draw.h like this:
#include "draw.h"
void rendererFunction(Man *man)
{
draw(man->x, man->y, man->w, man->y);
}
Then what if I also created a update.h (and update.c for implementation) file that will update his position? Do I really have to connect the draw.h to update.h just to get my type defined object? Or is there any other way of sharing type defined object without repeating the definition when I include the .h's in my main??
I hope someone can help me. Thanks
A common idiom in c header files are inclusion guards. These use the preprocessor to prevent things like multiple definitions. They look something like :
#ifndef MYMODULE_MYFILENAME_H
#define MYMODULE_MYFILENAME_H
.... Your definitions....
#endif
This allows you to always include the header containing what a given file is using, even if some other file happens to also include the header you need. Indeed this is good practice so that if later you refactor a header file to no longer include the file you need, some seemingly unrelated code doesn't start failing to compile.
In your update.h, just include "myTypeDef.h". Like what you do with draw.h and draw.c;
Or in your draw.h and update.h, just use sentence struct Man; to declare the struct. And in your draw.c and update.c use include "MyTypeDef.h".
I am actually trying to use a variable that is initialized in a header file(say x.h) and want to use same variable inside inlined code in the same header file. The same variable is modified in another file (say y.c). How can i do this ? I would like to know a good way of doing this.
Using the extern reserved word.
Never create variables in '.h' files, it's a bad practice that leads to bugs. Instead, declare them as extern everywhere you need to use them and declare the variable itself only in a single '.c' file where it will be instantiated, and linked to from all the other places you use it.
You can declare the global variable in the header file as extern, and then define it inside a code-module (i.e., ".c" file). That way you won't end up with multiple definition errors thrown by the linker.
So for example in your header file, a globally available int named my_global_var would have a declaration in a .h file that looks like:
extern int my_global_var;
Then inside a single .c file somewhere you would define and initialize it:
int my_global_var = 0;
Now you can use my_global_var in any other code module that includes the appropriate header file and links with the proper .c file containing the definition of the global variable.
I am new in programming in C, so I am trying many different things to try and familiarize myself with the language.
I wrote the following:
File q7a.h:
static int err_code = 3;
void printErrCode(void);
File q7a.c:
#include <stdio.h>
#include "q7a.h"
void printErrCode(void)
{
printf ("%d\n", err_code);
}
File q7main.c:
#include "q7a.h"
int main(void)
{
err_code = 5;
printErrCode();
return 0;
}
I then ran the following in the makefile (I am using a Linux OS)
gcc –Wall –c q7a.c –o q7a.o
gcc –Wall –c q7main.c –o q7main.o
gcc q7main.o q7a.o –o q7
the output is 3.
Why is this happening?
If you initialize a static variable (in fact any variable) in the header file, so if 2 files include the same header file (in this case q7.c and q7main.c) the linker is meant to give an error for defining twice the same var?
And why isn't the value 5 inserted into the static var (after all it is static and global)?
Thanks for the help.
static means that the variable is only used within your compilation unit and will not be exposed to the linker, so if you have a static int in a header file and include it from two separate .c files, you will have two discrete copies of that int, which is most likely not at all what you want.
Instead, you might consider extern int, and choose one .c file that actually defines it (i.e. just int err_code=3).
When you declared a variable as a static it has only scope within a file ie.., it can be accessed only within a file.
When you declare a static variable in a header file and include this header file in two .c file, then you are creating two different
memory for two different ".c" files
When you print err_code directly in the Main function you would see its value as 5 instead 3,
but you are calling a function printErrCode which is defined in a different file "q7a.c" for which err_code has a different memory location
in which err_code memory is still 3 and it not updated and that is why you are getting the value as 3 instead of 5.
Since two memory is created and err_code is considered as two different variables having different memory with different file scope you will not see any linking errors.
The static variables do not have external linkage which means they cannot be accessed outside the translation unit in which they are being defined. So in your case when q7.h is #include'ed in both translations units q7a.c and q7main.c ... two different copies exists in their corresponding .o files. That is why linker does not report error becuase both copies are not seen by linker while doing external symbol linkage.
While doing small research came to know that we can declare variable in Header file but in one of the source file includes that should have definition for that variable.
Instead if we define a variable in header file. in the source files where this header file included, definitions will be created which causes multiple definitions.
A static variable should be declared with in the file where we use it shouldn't be exposed to header file.
Hope I am giving right information. If I am wrong feel free to correct my statements in your comments.