Reputation: 336
May be my question is a little bit silly, but I can't get this code to work correctly. I have read similar question with answers, but there were a little bit different situation.
So, the question is: "Is it possible to get static variable value in inline functions?"
I code the program for microcontroller with small amount of memory and low speed. For some reason I need to make some functions inline (to save memory and time of execution). This is the problem description:
For example, I have three files in my program: main.c
, some.h
, some.c
.
In some.c
:
#include "some.h"
static int foo;
[... other functions that uses "foo"]
In some.h
:
#ifndef SOME_H_
#define SOME_H_
static int foo;
inline int __attribute__((always_inline))
get_foo(void){
return foo;
}
#endif
In main.c
:
#include "some.h"
int bar;
int main(void){
bar = get_foo();
return bar;
}
When I tried to compile, I'm getting following:
warning: 'foo' is static but used in inline function 'get_foo' which is not static
When I tried to put extern static int foo;
to some.h
, I got:
error: multiple storage classes in declaration specifiers
error: static declaration of 'foo' follows non-static declaration
I need to get values of "static" variables from external file in "inline" functions. I want to keep them "static" to separate from main code, as local variable. Is it a way to do this correctly?
Thank you in advance!
EDIT:
To be more clear: I need to get the the value of a variable in one file (some.c
) in another file (main.c
) by inline function. If the function get_foo()
is NOT "inline", everything works perfect, but it takes more memory and execution time.
Upvotes: 2
Views: 1283
Reputation: 36391
Your request for inlining is in some way incompatible with the internal linkage of the variable.
You have a static variable which means that this variable is only visible in that file.
On the other you have an inline function. If this function would be inlined somewhere else than in this file, then it would refer to another foo variable, which is probably not what you want; at least the compiler warns you about it. It suggests you to change the visibility of that function to static, as to only be available in the same entity than foo.
In another try, you add extern, but this is incompatible with static visibility, choose, extern or static. Read about linkage and visibility rules in C language, for more information.
You also suggest to add foo in some header, which is not a good idea, headers are reserved for declaration of objects not definition. You are encouraged to add type definition in headers, external declaration of objects; but discouraged from define objects (while not strictly forbidden, this would too often lead to clashes).
Upvotes: 0
Reputation: 78903
If you want to have your variable static
in a specific translation unit, an inline
function that would "live" in another translation unit would never be able to access this static
object. That is why access to static
objects from inline
functions is not allowed, the semantics of what that would mean is simply not clear.
You can either define an extern
function that provides the value of its TU where it is defined, or static
function(s) with different instantiations in every TU that provide just the value, there.
Upvotes: 0
Reputation: 342
There is no point for defining a static variable in a header file. Every time you include that header file you will get a new fresh copy of the variable...
Why you don't just define it as
extern int foo;
This way you will have the variable available from any source that included the header
Upvotes: 1
Reputation: 1395
In this case, static
means the variable foo will be a different foo in each source file. If you change static int foo;
to extern int foo;
in the header and then have a single int foo;
in one .c file, I think the compiler warning will go away and your inline function will work fine.
At file scope, static
hides a name from the linker so you effectively have different variables in each file.
extern static int foo;
is just a mistake. It means, there is something called foo in some other file that I want you to link to, but the name is hidden. The compiler is just saying "forget it".
Upvotes: 2