Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

again "undefined symbol" in runtime, c++

I have a data type and I can instantiate a variable of that type. like this:

FetchAddr faddr(VirtualMemoryAddress( 0x0a ));

The definition of FetchAdr is:

struct FetchAddr {
   VirtualMemoryAddress theAddress;
   FetchAddr(VirtualMemoryAddress anAddress)
     : theAddress(anAddress)
   { } 
};

Now I have a class that faddr is a private (or public) variable

class FLEXUS_COMPONENT(BPred)  {
   static FetchAddr faddr;
   public:
     FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
        : base( FLEXUS_PASS_CONSTRUCTOR_ARGS )
     {
        faddr = VirtualMemoryAddress( 0x0a );
     }
   ...
}

Assume the macros are defined properly.

The code compiles and links without any problem. However when I start the program, it says:

 "undefined symbol: _ZN6nBPred14BPredComponent8faddr"

it says there no symbol for faddr.

any idea about that?

like image 286
mahmood Avatar asked Jul 31 '11 12:07

mahmood


People also ask

How do you fix undefined symbol in C?

The error is produced by the linker, ld . It is telling you that the symbol pow cannot be found (is undefined in all the object files handled by the linker). The solution is to include the library which includes the implementation of the pow() function, libm (m for math).

What does undefined symbol mean in C?

UNDEFINED SYMBOL AT COMPILE TIME An undefined symbol at compile time indicates that the named identifier was used in the named source file, but had no definition in the source file. This is usually caused by a misspelled identifier name, or missing declaration of the identifier used.

What is undefined symbol in C Plus Plus?

A symbol remains undefined when a symbol reference in a relocatable object is never matched to a symbol definition. Similarly, if a shared object is used to create a dynamic executable and leaves an unresolved symbol definition, an undefined symbol error results.


1 Answers

When you declare a static member you also have to define it somewhere, like in a .cpp file. And also remember to link to this file.


Problem number 2 - FetchAddr doesn't have a default constructor.

If you need to have faddr as a static member of the class, you also need to give it a value when it is defined, like:

FetchAddr FLEXUS_COMPONENT(BPred)::faddr(VirtualMemoryAddress( 0x0a ));

That creates an faddr that is shared by all FLEXUS_COMPONENT(BPred) objects.

If you rather have it that each object has its own copy of the faddr variable, you can make it non-static and initialize it in the constructor:

class FLEXUS_COMPONENT(BPred)  {
   FetchAddr faddr;
   public:
     FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
        : base( FLEXUS_PASS_CONSTRUCTOR_ARGS ),
          faddr(VirtualMemoryAddress( 0x0a ))
     { }
   // ...
};
like image 127
Bo Persson Avatar answered Oct 20 '22 10:10

Bo Persson