Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable has internal linkage but is not defined

Tags:

c++

linkage

I have this .h file:

namespace{

class Invariant{
public:
  Invariant(z3::expr e,Instruction *i):Expr(e),I(i){
    DenseMap<Instruction*,Invariant*> FunMap = Invariants[F];
  }

private:
  //static map
  static DenseMap<Function*, DenseMap<Instruction*,Invariant*> >Invariants;

};
}//end of anonymous namespace

When I compile clang says:

Invariant.h:46:65: warning: variable '<anonymous namespace>::Invariant::Invariants' has internal linkage but is not defined
  static DenseMap<Function*, DenseMap<Instruction*,Invariant*> >Invariants;
                                                                ^
Invariant.h:26:48: note: used here
    DenseMap<Instruction*,Invariant*> FunMap = Invariants[F];

What's the problem here?

like image 940
Giacomo Tagliabue Avatar asked Apr 08 '13 04:04

Giacomo Tagliabue


1 Answers

Just define it. After the class definition but before the end of the anonymous namespace add this line:

DenseMap<Function*, DenseMap<Instruction*,Invariant*> > Invariant::Invariants;

This will create the static member in every translation unit that includes this header (that's okay because it's in the anonymous namespace, which is unique to each translation unit). That's probably not what you want, but that follows from defining Invariant in an anonymous namespace. If you use a named namespace instead, you can put the definition of Invariants into a source file and have only one object shared by all the code.

like image 133
Pete Becker Avatar answered Sep 22 '22 08:09

Pete Becker