Monday 15 July 2013

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



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

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

feel free edit/add own.

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

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

physical source file characters mapped, in implementation-defined manner, basic source character set (introducing new-line characters end-of-line indicators) if necessary. [snip] each instance of backslash character (\) followed new-line character deleted, splicing physical source lines form logical source lines. [snip] the source file decomposed preprocessing tokens (2.5) , sequences of white-space characters (including comments). [snip] preprocessing directives executed, macro invocations expanded, , _pragma unary operator expressions executed. [snip] each source character set fellow member in character literal or string literal, each escape sequence , universal-character-name in character literal or non-raw string literal, converted corresponding fellow member of execution character set; [snip] adjacent string literal tokens concatenated. white-space characters separating tokens no longer significant. each preprocessing token converted token. (2.7). resulting tokens syntactically , semantically analyzed , translated translation unit. [snip] translated translation units , instantiation units combined follows: [snip] all external entity references resolved. library components linked satisfy external references entities not defined in current translation. such translator output collected programme image contains info 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 lastly 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 msvs, 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 utilize .lib (if any).

similar mechanism exist other compilers/platforms.

common error messages error lnk2001, error lnk1120, error lnk2019 msvs , undefined reference 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; }

would generate next 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 msvs:

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:

failure link against appropriate libraries/object files or compile implementation files declared , undefined variable or function. common issues class-type members template implementations not visible. symbols defined in c programme , used in c++ code. incorrectly importing/exporting methods/classes across modules/dll. (msvs specific) circular library dependency undefined reference `winmain@16' interdependent library order multiple source files of same name mistyping or not including .lib extension when using #pragma (msvc) problems template friends

c++ linker-error undefined-reference c++-faq unresolved-external

No comments:

Post a Comment