1

I have driver.h which contains:

namespace org::lib{
  extern bool masterBool;
}

And a library.h which contains an anonymous namespace that defines masterBool:

namespace {
    bool masterBool = false;
    std::string otherFunction(){ 
        //....
    }
}

My driver.cpp which calls the otherFunction() and masterBool:

#include driver.h
#include library.h

namespace org::lib{

 void function(){
     otherFunction();
     if (masterBool){
        //.....
     }
 }
}

Upon compiling, I'm getting a undefined symbol: org::lib::masterBool in my driver.cpp. However, otherFunction() call did not such an error, even though they are both defined in the anonymous namespace of library.h

What am I missing here?

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
user1008636
  • 2,989
  • 11
  • 31
  • 45
  • Is that anonymous namespace somehow wrapped into a `namespace org::lib`? – UnholySheep Nov 28 '22 at 20:53
  • Where are you defining `bool org::lib::masterBool`? – Ted Lyngmo Nov 28 '22 at 20:54
  • @UnholySheep, yes it is, forgot to update my code, updated now. – user1008636 Nov 28 '22 at 20:54
  • 4
    The member of the anonymous namespace and the member of `org::lib` are two different variables. You need to define the variable in the same namespace as its declaration. – NathanOliver Nov 28 '22 at 20:54
  • @NathanOliver just updated the question, the anonymous namespace exists under an outer namespace of org::lib – user1008636 Nov 28 '22 at 20:56
  • @TedLyngmo i'm defining it in library.h – user1008636 Nov 28 '22 at 20:56
  • Why are you making it this complicated? Why do you have an extern declaration of `masterBool` in one header and a translation unit specific version in another header? Define it in `driver.cpp` and nowhere else - if it's supposed to be the same `bool`. – Ted Lyngmo Nov 28 '22 at 20:57
  • `namespace {` [anonymous namespace](https://en.cppreference.com/w/cpp/language/namespace#Unnamed_namespaces). Effectively makes everything in it `static`, This can put it at odds with `extern`. – user4581301 Nov 28 '22 at 20:59
  • @TedLyngmo masterBool needs to be defined in other cpp files as well. – user1008636 Nov 28 '22 at 20:59
  • @user1008636 Is `masterBool` supposed to be referring to the _same_ variable everywhere - or different variables with the same name? – Ted Lyngmo Nov 28 '22 at 21:00
  • @user4581301 so i cannot put the definition of masterBool into an anonymous namespace ? – user1008636 Nov 28 '22 at 21:00
  • 4
    @user1008636 Why would you want to? The point of an anonymous namespace is to make a symbol unlinkable. – Spencer Nov 28 '22 at 21:04
  • `namespace {` ... but why? – Eljay Nov 28 '22 at 21:05
  • 2
    Can't say I've ever tried, but even if we eliminate the complexity of having other files we wind up with this: https://godbolt.org/z/dEE7Y8TnG . Note the `bool org::lib::{anonymous}::masterBool` vs the `bool org::lib::masterBool` in the error messages.. Cleary they are seen as different variables. – user4581301 Nov 28 '22 at 21:06
  • "_masterBool needs to be defined in other cpp files as well._": Every non-`inline` variable or function can have only one definition. It can't have multiple definitions in multiple translation units. What is the reason that you assume you need that? – user17732522 Nov 28 '22 at 21:09
  • Someone better with the nitty-gritty details of the language can provide a really interesting answer on the "Why" and drop Standard quotes, but me, I just learned something new about C++, so +1. – user4581301 Nov 28 '22 at 21:09
  • For completeness, here's the output from the big 3 compilers: https://godbolt.org/z/c4GG9YcGr – user4581301 Nov 28 '22 at 21:12
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Nov 29 '22 at 00:14

1 Answers1

1

::org::lib::masterBool is not the same variable as ::(unnamed)::masterBool and is never defined. (They wouldn't be the same even if the unnamed namespace appeared inside org::lib.) There aren't two otherFunctions (in any one translation unit—see below), so that problem can't occur there.

More generally, don't use unnamed namespaces in header files; even the rare recommendations are better served by inline variables (or some emulation of them prior to C++17).

Davis Herring
  • 36,443
  • 4
  • 48
  • 76