Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extern on a static function in C++

I don't know why this is driving me nuts but it is. I have a function defined and forward declared in main.

static void myFunc(int x);

static void myFunc( int x)
{
   //do stuff
}

main()

I want to use myFunc(int x) in another class. So I would think all I have to do is extern static void myFunc(int x) within that classes header and then just call it where I need to in the class definition, but it won't work.

What am I doing wrong?

Thanks

like image 636
Dixon Steel Avatar asked Oct 27 '11 18:10

Dixon Steel


People also ask

Can you extern a static function in C?

Static variables in C have the following two properties: They cannot be accessed from any other file. Thus, prefixes “ extern ” and “ static ” cannot be used in the same declaration. They maintain their value throughout the execution of the program independently of the scope in which they are defined.

Can we make static function as an extern?

You cannot use extern and static together they are mutually exclusive. You need to use only extern if you need External Linkage.

What is static extern?

static means a variable will be globally known only in this file. extern means a global variable defined in another file will also be known in this file, and is also used for accessing functions defined in other files. A local variable defined in a function can also be declared as static .

Can I extern a function in C?

The extern keyword in C and C++ extends the visibility of variables and functions across multiple source files. In the case of functions, the extern keyword is used implicitly. But with variables, you have to use the keyword explicitly.


1 Answers

You cannot use extern and static together they are mutually exclusive.

static means Internal Linkage
extern means External Linkage

You need to use only extern if you need External Linkage.

Good Read:
what is external linkage and internal linkage in c++?

like image 152
Alok Save Avatar answered Oct 08 '22 14:10

Alok Save