16

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

Dixon Steel
  • 1,021
  • 4
  • 12
  • 27

1 Answers1

28

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++?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Ok, that's what I thought, but when I remove the static from the definition, it still will not build. – Dixon Steel Oct 27 '11 at 18:29
  • If you maintain the declaration as `static void myFunc(int);` the function *will be* `static` regardless of the presence of `static` in the definition. – David Rodríguez - dribeas Oct 27 '11 at 18:30
  • Thanks, I have it, I removed the static, and also I had it declared wrong in the header. – Dixon Steel Oct 27 '11 at 18:37
  • @DixonSteel:You remove the `static` keyword from both the **declaration** and **definition** of the function, and then you can access it using `extern` from another source file(Translation Unit actually) – Alok Save Oct 27 '11 at 18:37