c++ - What is an undefined reference/unresolved external symbol error and how do I fix it? -


what undefined reference/unresolved external symbol errors? common causes , how fix/prevent them?

feel free edit/add own.

compiling c++ program takes place in several steps, specified 2.2 (credits keith thompson reference):

the precedence among syntax rules of translation specified following phases [see footnote].

  1. physical source file characters mapped, in implementation-defined manner, basic source character set (introducing new-line characters end-of-line indicators) if necessary. [snip]
  2. each instance of backslash character (\) followed new-line character deleted, splicing physical source lines form logical source lines. [snip]
  3. the source file decomposed preprocessing tokens (2.5) , sequences of white-space characters (including comments). [snip]
  4. preprocessing directives executed, macro invocations expanded, , _pragma unary operator expressions executed. [snip]
  5. each source character set member in character literal or string literal, each escape sequence , universal-character-name in character literal or non-raw string literal, converted corresponding member of execution character set; [snip]
  6. adjacent string literal tokens concatenated.
  7. white-space characters separating tokens no longer significant. each preprocessing token converted token. (2.7). resulting tokens syntactically , semantically analyzed , translated translation unit. [snip]
  8. translated translation units , instantiation units combined follows: [snip]
  9. all external entity references resolved. library components linked satisfy external references entities not defined in current translation. such translator output collected program image contains information needed execution in execution environment. (emphasis mine)

[footnote] implementations must behave if these separate phases occur, although in practice different phases might folded together.

the specified errors occur during last stage of compilation, commonly referred linking. means compiled bunch of implementation files object files or libraries , want them work together.

say defined symbol a in a.cpp. now, b.cpp declared symbol , used it. before linking, assumes that symbol defined somewhere, doesn't yet care where. linking phase responsible finding symbol , correctly linking b.cpp (well, object or library uses it).

if you're using microsoft visual studio, you'll see projects generate .lib files. these contain table of exported symbols, , table of imported symbols. imported symbols resolved against libraries link against, , exported symbols provided libraries use .lib (if any).

similar mechanisms exist other compilers/ platforms.

common error messages error lnk2001, error lnk1120, error lnk2019 microsoft visual studio , undefined reference to symbolname gcc.

the code:

struct x {    virtual void foo(); }; struct y : x {    void foo() {} }; struct {    virtual ~a() = 0; }; struct b: {    virtual ~b(){} }; extern int x; void foo(); int main() {    x = 0;    foo();    y y;    b b; } 

will generate following errors gcc:

/home/abisfw/ccvvuhox.o: in function `main': prog.cpp:(.text+0x10): undefined reference `x' prog.cpp:(.text+0x19): undefined reference `foo()' prog.cpp:(.text+0x2d): undefined reference `a::~a()' /home/abisfw/ccvvuhox.o: in function `b::~b()': prog.cpp:(.text._zn1bd1ev[b::~b()]+0xb): undefined reference `a::~a()' /home/abisfw/ccvvuhox.o: in function `b::~b()': prog.cpp:(.text._zn1bd0ev[b::~b()]+0x12): undefined reference `a::~a()' /home/abisfw/ccvvuhox.o:(.rodata._zti1y[typeinfo y]+0x8): undefined reference `typeinfo x' /home/abisfw/ccvvuhox.o:(.rodata._zti1b[typeinfo b]+0x8): undefined reference `typeinfo a' collect2: ld returned 1 exit status 

and similar errors microsoft visual studio:

1>test2.obj : error lnk2001: unresolved external symbol "void __cdecl foo(void)" (?foo@@yaxxz) 1>test2.obj : error lnk2001: unresolved external symbol "int x" (?x@@3ha) 1>test2.obj : error lnk2001: unresolved external symbol "public: virtual __thiscall a::~a(void)" (??1a@@uae@xz) 1>test2.obj : error lnk2001: unresolved external symbol "public: virtual void __thiscall x::foo(void)" (?foo@x@@uaexxz) 1>...\test2.exe : fatal error lnk1120: 4 unresolved externals 

common causes include:


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -