87

I'm getting an error from the following code using C++.

Main.cpp

#include "file.h"

int main()
{
   int k = GetInteger();
   return 0;
}

File.h

static int GetInteger();

File.cpp

#include "file.h"

static int GetInteger()
{
   return 1;
}

The error I get:

Error C2129: static function 'int GetInteger(void)' declared but not defined.

I've read the famous article "Organizing Code File in C and C++", but don't understand what is wrong with this code.

3
  • 2
    How do you link it ? "gcc -o Test Main.cpp File.cpp -lstdc++" or somewhere in XCode/VisualStudio/Eclipse ? Commented May 30, 2012 at 8:38
  • 2
    What @ViktorLatypov said. Show us how you're compiling it.
    – Brady
    Commented May 30, 2012 at 8:40
  • @ViktorLatypov: I'm encountering this by selecting the file in Visual Studio 2013 in a C++/CLI project and clicking the compile button. (Compile button for a C++ button doesn't link, and shouldn't encounter this error, but maybe C++/CLI is different?) (Oh wait, just saw top answer. I'd converted a static class to a namespace >.< Forgot.) Commented Aug 11, 2014 at 21:33

7 Answers 7

155

In C++, static at global/namespace scope means the function/variable is only used in the translation unit where it is defined, not in other translation units.

Here you are trying to use a static function from a different translation unit (Main.cpp) than the one in which it is defined (File.cpp).

Remove the static and it should work fine.

29

Change

static int GetInteger();

to

int GetInteger();

static in this case gives the method internal linkeage, meaning that you can only use it in the translation unit where you define it.

You define it in File.cpp and try to use it in main.cpp, but main doesn't have a definition for it, since you declared it static.

9

Because in this case, static means that the name of the function has internal linkage; that GetInteger in one translation unit is unrelated to GetInteger in any other translation unit. The keyword static is overloaded: in some cases, it affects lifetime, and in others, binding. It's particularly confusing here, because "static" is also the name of a lifetime. Functions, and data declared at namespace scope, always have static lifetime; when static appears in their declaration, it causes internal binding, instead of external.

5

functions declared as static arelocal to the containing file. Therefore, you have to define the function in the same file as the ones who call it. If you want to make it callable from other file, you must NOT declare it as static.

3

From my understanding, static functions are name mangled with the filename in which they are defined so when you include file.h in main.cpp, GetInteger() get mangled with main.cpp though you have defined GetInteger() in file.cpp but since it is static it gets mangled too and linker cannot find the definition of GetInteger() as no function by this name exists.

I believe lesson learnt is don't declare static functions in headerfile as are not intended to be a part of interface.

3

If everything is in the same translation unit it should work. You probably did not compile File.cpp into the same unit as Main.cpp.

g++ -Wall File.cpp Main.cpp

If each file is compiled separately the function must be made extern to be used from a different translation unit.

extern int GetInteger();

which is the same as

int GetInteger();
1
  • He's using Visual Studio, not g++
    – Brady
    Commented May 30, 2012 at 9:13
1

Consider using namespaces...

for logical sections of code that don't require internal member variables. Meaning, your GetInteger() function doesn't require an internal constant variable to be referenced. In order to keep your functions organized in your code, consider using namespaces. This keeps function names from conflicting (which will save your hours of headache with possible LNK errors). The code still works in the way you've set up and still works within the confines of the accepted answer. Additionally, your namespace name can help you quickly debug.

Main.cpp

#include "file.h"

int main()
{
   int k = HELPERS::GetInteger();
   return 0;
}

File.h

namespace HELPERS
{
    int GetInteger();
}

File.cpp

#include "file.h"

int HELPERS::GetInteger()
{
   return 1;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.