function declared static but never defined - c

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

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.

#include "another_source.c", use inline function( ) there, then does the function( ) become inline as well?

Let's say I have two files named "AA.c", "BB.c"
/* in AA.c */
inline void AA(void) __attribute__((always_inline));
void AA()
{
/* do something */
}
and then
/* in BB.c */
#include "AA.c"
extern void funcAA(void);
int main(void)
{
funcAA();
return 0;
}
does funcAA( ) also become inline???
no matter the answer is yes or no, could you explain some more about the under the hood??
including a .c file is equivalent of copying and pasting the file contents directly in the file which includes that, exactly like if the function was directly defined in the including file.
You can see what the compiler is going to compile by trying to compile your file with -E flag (preprocessor output). You'll see your function pasted-in.
So it will be inline just because of the inline keyword, and forced with the always_inline attribute even if the compiler would have refused to inline it because of function size for instance.
Word of advice: know what you're doing when including a .c file from another one. Some build systems/makefiles just scan the directories looking for files called *.c so they can compile them separately. Putting a possibly non-compiling C file there can make the build fail, and if it builds, you could have duplicate symbols when linking. Just don't do this.
If you want to do this, put your function in a .h file and declare it static so it won't fail the link if included in many .c files (each function will be seen as different)

Declaring,initializing and using a global variable in same header file

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.

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

Is there a way to define C inline function in .c file rather than .h file?

As I know, C inline function body should be defined in .h file
because it causes an error 'function-name used but never defined" if body defined in .c file.
Is this the regular way? Or how to define inline function body in .c file?
Each .c file is compiled independently into .o output. If you define the inline function in a .c file, other source files cannot see such function, so that cannot be inlined.
Therefore the inline function should be in the .h file to allow the code to be shared.

Resources