1

I have this problem with a static functions declared in a header file. Say I have this file "test.cpp":

#include <cstdlib>
#include "funcs.hpp"

...do some operations...

void *ptr = my_malloc(size);

...do some operations...

The "funcs.hpp" file contains:

#include <ctime>
#include "../my_allocator.hpp" // my_malloc is defined here as a 
                               // global static function

...some functions...

static int do_operation() {
    // variables declaration
    void *p = my_malloc(size);

   // ...other operations
}

when I compile my program, I get an error 'my_malloc' was not declared in this scope, referred to the one used in the "funcs.hpp" file, while it doesn't complain with the one used in "test.cpp".

The function in question is defined as a global static function within the header "my_allocator.hpp", and all it does is to get an instance of the allocator object and use the malloc function:

static void * my_malloc(size_t size) {
    return MYAllocator::instance()->malloc(size);
}

I'm not sure whether this problem is related to the scope of a function declared as static global inside a header, but what looks weird to me is that it complains in one case and not in the other: when I use it in a static function in a header (funcs.hpp) that includes the header where it is declared, it results in the error "not declared in this scope"; while it is OK when used from a file that includes the header "funcs.hpp". Where am I getting wrong?

Faabiioo
  • 89
  • 1
  • 10
  • "`my_malloc` was not declared in this scope" is not a linker error, but a name lookup error. I.e. your compiler cannot find the name `my_malloc`. Please provide an [SSCCE](http://sscce.org), if possible. – dyp Sep 12 '13 at 22:05
  • actually, the compiler does find the name: it reports the name as an error when I use `my_malloc` in a the "funcs.hpp" header; no error when used in "test.cpp". it seems to me that the included header ("my_allocator.hpp") somehow is skipped within the "funcs.hhp" file, but properly used in "test.cpp" that includes "funcs.hpp". and I can't really get why – Faabiioo Sep 14 '13 at 12:06

1 Answers1

1

The static specifier specifies that the symbol (variable or function) will be visible only in this translation unit. I think thats not the case you want, so remove the static specifier.
Note what the compiler is saying: "my_malloc was not declared in this scope". Thats exactly what you specified with static: Internal linkage

Also note that this use of the static keyword was deprecated in favor of unnamed namespaces.

Community
  • 1
  • 1
Manu343726
  • 13,969
  • 4
  • 40
  • 75
  • 1
    Its not deprecated for functions, and that status was revoked in C++11 AFAIK – paulm Sep 12 '13 at 21:08
  • The compiler doesn't care about linkage, that's a problem of the linker. What I want to say with that is: that error is either misleading or not related to linkage. Also note that internal linkage avoids name clashes, which is good for global functions (if they're small enough not to require separate compilation). – dyp Sep 12 '13 at 22:09
  • uhm... removing the static keyword does not much change the problem. Everything I use that is declared in "my_allocator.hpp" is not seen by the compiler. even if I create a `new` object (like `OtherClass *cl = new OtherClass()`) from a class defined in that header, I get the same error response. – Faabiioo Sep 14 '13 at 12:12