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.
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.
My current make to to SDK got a error which says wan_name is not declared in linux-2.6.21/net/netfilter/nf_conntrack_core.c.
It is declared as extern in nf_conntrack_core.c
I am reconfiguring Linux kernel as per my requirements.
While tracing wan_name variable I found it is declared and used in linux-2.6.21/net/ipv4/ip_tables.c and also declared extern and used in linux2.6.21/net/ipv4/ip_conntrack_core.c
So it is like wan_name is declared in net/ipv4/ directory inside .c file and used as extern by other .c file in same directory. But net/netfilter/ directory .c files want to access that variable.
is that possible ?
what could be best solution to fix this bug ??
As per my knowledge extern allow you to access global variable declared in the same directory .c files. what if other directory .c file want to access that variable.
Any thoughts ?
Your thought is not correct . You can you can use
extern
even if the c files are in different directory.
I have a header file suppose abc.h, where i have function declaration as:
static int function1();
I have included this header file in abc.c and has defined the function and used it.
static int function1()
{
< function definition>
}
After compiling I am getting warning:
warning: function1 declared static but never defined
How can I remove warning, without removing static.
Thanks.
A static function can be declared in a header file, but this would cause each source file that included the header file to have its own private copy of the function, which is probably not what was intended.
Are u sure u haven't included the abc.h file in any other .c files?
Because declaring a function as static, requires the function to be defined in all .c file(s) in which it is included.
Good practice: Declare static functions in the source file they are defined in (please also provide prototype), since that's the only file they are visible in.
This way, the function is only visible to that file, such visibility issues can reduce possible code conflict! So, just provide the prototype and the static function definition in the .c file. Do not include the static function in the header file; the .h file is for external consumption.
Duplicate: Static functions in C
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
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.