You're somehow not actually compiling testRect.cpp (or maybe not linking in testRect.o after it is compiled). Like suggested on SO, you need to look at the actual compilation/linking process you're invoking. Answer from TheSkiGeek on reddit.com
🌐
Reddit
reddit.com › r/cplusplus › undefined symbol issue, (linker issue) ?
r/Cplusplus on Reddit: Undefined symbol issue, (linker issue) ?
August 20, 2021 -

I have been working on a problem mately, and I'm stuck with an error that I did not find how to resolve. Please, can you help me ?

All the details of it has been posted here : https://stackoverflow.com/questions/68841669/undefined-symbols-for-architecture-x86-64-linker-issue?noredirect=1#comment121663792_68841669

Top answer
1 of 4
4

The error is not related to UNIX/Windows/any other OS, but rather to the languages themselves. It is actually rather simple to diagnose with the information that compilers provide. Usually they will tell you what symbol is missing and sometimes where it is being used. The main reasons for a symbol to be missing are:

  • You have declared but never defined it
  • You have defined it, but did not add the compiled symbol (object file/library) to the linker
  • It is external and you forgot to link the library, or you are linking an invalid version, or in the wrong order.

The first one is a little trickier if you intended to define the symbol but did not match the declaration (declared void foo( int, double );, but defined void foo( double, int ). As with all other cases, the compiler will tell you the exact signature that it is looking for, make sure that you have defined that symbol, and not something close or similar, a particular corner case can be if you are using different calling conventions in the declaration and the definition, as they will look very similar in code.

In the case of libraries external code the complexity is in identifying what library needs to be linked for that symbol to be added, and that comes from the documentation of the lib. Beware that with static libraries the order of the libs in the linker command line affects the result.

To help you in finding what symbols are actually defined you can use nm (gcc, which is common among unix systems). So basically you can run nm against the object files/libs that you are linking and search for the symbol that the linker is complaining about. This will help in cases where the order is what makes the difference (i.e. the symbol is there, but the linker skipped it).

At runtime (thanks to Matthieu M. for pointing it out) you might have similar issues with dynamic libraries, if the wrong version of a library is found in the LD_LIBRARY_PATH you might end up with a library that does not have a required symbol.

2 of 4
2

Although they can be platform dependent, I have some "more complex" instances of some of the points from Andreas and David:

  • When dealing with shared libraries (.so or.dll) and linking against symbols which are not exported (dllimport/dllexport on Windows and visibility("default") with GCC on *nix)
  • Or similar: Linking against the static lib, while expecting a shared lib or vice versa. This one is bit similar to Mathieu's comment about linking against another, unexpected version of the library.
  • Creating a pure virtual classs and not providing an implementation for at least one method (causing no vtable to be available).
  • Actually a more complex case of declaring but not defining: The linking errors you can get when dealing with large, nested templates. Finding out what was not defined can be difficult with large error messages.
Discussions

dynamic linking - finding undefined symbol - Unix & Linux Stack Exchange
I am trying to run a program whose source I downloaded and compiled. When I try to run the program I keep getting the message: unable to load undefined symbol _z15InvalidateImageSs I am trying to More on unix.stackexchange.com
🌐 unix.stackexchange.com
March 31, 2015
how to debug undefined symbol
Say, definition of class A in A.h contains a function f17 that is not implemented in A.cpp. Binder will pass, pybind11 will pass, compiler will pass, linker will pass - but the Python import statem... More on github.com
🌐 github.com
1
1
March 25, 2022
c++ - What is an undefined reference/unresolved external symbol error and how do I fix it? - Stack Overflow
What are undefined reference/unresolved external symbol errors? What are common causes, and how do I fix and prevent these errors? More on stackoverflow.com
🌐 stackoverflow.com
error: undefined symbol ... during make
I get this error during make process of my project. the symbols are well defined in the external header .h file. The .h files is well included in my main .c... More on forum.qt.io
🌐 forum.qt.io
22
0
March 18, 2020
🌐
Cplusplus
cplusplus.com › forum › general › 57873
undefined symbol - C++ Forum
December 18, 2011 - Weird, I have a series of database which are struct's and all of those are contained in one include file. Those structs are used by multiple cpp's. Originally I was getting a duplicate symbol error so I rewrote it to use extern and now it is generating an undefined symbol error.
🌐
Oracle
docs.oracle.com › cd › E19120-01 › open.solaris › 819-0690 › chapter2-9 › index.html
Undefined Symbols (Linker and Libraries Guide)
After all of the input files have been read and all symbol resolution is complete, the link-editor searches the internal symbol table for any symbol references that have not been bound to symbol definitions. These symbol references are referred to as undefined symbols.
Find elsewhere
Top answer
1 of 16
1035

Say you have the following code:

// a.cpp
int get() { return 0; }
// b.cpp
int get(); // usually, one doesn't write this directly, but gets these
           // declarations from included header files
int x = get();

When compiling b.cpp, the compiler simply assumes that get() symbol was defined somewhere, but it doesn't yet care where. The linking phase is responsible for finding the symbol and correctly linking the object files produced from a.cpp and b.cpp.

If a.cpp didn't define get, you would get a linker error saying "undefined reference" or "unresolved external symbol".

C++ Standard Wording

Compiling a C++ program takes place in several phases specified in [lex.phases], the last of which is relevant:

9. All external entity references are resolved. Library components are linked to satisfy external references to entities not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment.

See Keith Thompson's answer for a summary of these phases.

The specified errors occur during this last stage of compilation, most commonly referred to as linking. It basically means that you compiled a bunch of source files into object files or libraries, and now you want to get them to work together.

Linker Errors in Practice

If you're using Microsoft Visual Studio, you'll see that projects generate .lib files. These contain a table of exported symbols, and a table of imported symbols. The imported symbols are resolved against the libraries you link against, and the exported symbols are provided for the libraries that use that .lib (if any).

Similar mechanisms exist for other compilers/ platforms.

Common error messages are error LNK2001, error LNK1120, error LNK2019 for Microsoft Visual Studio and undefined reference to symbolName for GCC.

The code:

struct X
{
   virtual void foo();
};
struct Y : X
{
   void foo() {}
};
struct A
{
   virtual ~A() = 0;
};
struct B: A
{
   virtual ~B(){}
};
extern int x;
void foo();
int main()
{
   x = 0;
   foo();
   Y y;
   B b;
}

will generate the following errors with GCC:

/home/AbiSfw/ccvvuHoX.o: In function `main':
prog.cpp:(.text+0x10): undefined reference to `x'
prog.cpp:(.text+0x19): undefined reference to `foo()'
prog.cpp:(.text+0x2d): undefined reference to `A::~A()'
/home/AbiSfw/ccvvuHoX.o: In function `B::~B()':
prog.cpp:(.text._ZN1BD1Ev[B::~B()]+0xb): undefined reference to `A::~A()'
/home/AbiSfw/ccvvuHoX.o: In function `B::~B()':
prog.cpp:(.text._ZN1BD0Ev[B::~B()]+0x12): undefined reference to `A::~A()'
/home/AbiSfw/ccvvuHoX.o:(.rodata._ZTI1Y[typeinfo for Y]+0x8): undefined reference to `typeinfo for X'
/home/AbiSfw/ccvvuHoX.o:(.rodata._ZTI1B[typeinfo for B]+0x8): undefined reference to `typeinfo for A'
collect2: ld returned 1 exit status

and similar errors with 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

  • Failure to link against appropriate libraries/object files or compile implementation files
  • Declared and undefined variable or function.
  • Common issues with class-type members
  • Template implementations not visible.
  • Symbols were defined in a C program and used in C++ code.
  • Incorrectly importing/exporting methods/classes across modules/dll. (MSVS specific)
  • Circular library dependency
  • undefined reference to `WinMain@16'
  • Interdependent library order
  • Multiple source files of the same name
  • Mistyping or not including the .lib extension when using the #pragma (Microsoft Visual Studio)
  • Problems with template friends
  • Inconsistent UNICODE definitions
  • Missing "extern" in const variable declarations/definitions (C++ only)
  • Visual Studio Code not configured for a multiple file project
  • Errors on Mac OS X when building a dylib, but a .so on other Unix-y systems is OK
  • Your linkage consumes libraries before the object files that refer to them
  • Your linkage contains C++ object files and C++ libraries originating from different C++ compilers
2 of 16
201

Class members:

A pure virtual destructor needs an implementation.

Declaring a destructor pure still requires you to define it (unlike a regular function):

struct X
{
    virtual ~X() = 0;
};
struct Y : X
{
    ~Y() {}
};
int main()
{
    Y y;
}
//X::~X(){} //uncomment this line for successful definition

This happens because base class destructors are called when the object is destroyed implicitly, so a definition is required.

virtual methods must either be implemented or defined as pure.

This is similar to non-virtual methods with no definition, with the added reasoning that the pure declaration generates a dummy vtable and you might get the linker error without using the function:

struct X
{
    virtual void foo();
};
struct Y : X
{
   void foo() {}
};
int main()
{
   Y y; //linker error although there was no call to X::foo
}

For this to work, declare X::foo() as pure:

struct X
{
    virtual void foo() = 0;
};

Non-virtual class members

Some members need to be defined even if not used explicitly:

struct A
{ 
    ~A();
};

The following would yield the error:

A a;      //destructor undefined

The implementation can be inline, in the class definition itself:

struct A
{ 
    ~A() {}
};

or outside:

A::~A() {}

If the implementation is outside the class definition, but in a header, the methods have to be marked as inline to prevent a multiple definition.

All used member methods need to be defined if used.

A common mistake is forgetting to qualify the name:

struct A
{
   void foo();
};

void foo() {}

int main()
{
   A a;
   a.foo();
}

The definition should be

void A::foo() {}

static data members must be defined outside the class in a single translation unit:

struct X
{
    static int x;
};
int main()
{
    int x = X::x;
}
//int X::x; //uncomment this line to define X::x

An initializer can be provided for a static const data member of integral or enumeration type within the class definition; however, odr-use of this member will still require a namespace scope definition as described above. C++11 allows initialization inside the class for all static const data members.

🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › undefined-symbol-when-trying-to-compile-c-256994
"undefined symbol" when trying to compile c?
November 19, 2004 - hello, i been having this problem for about a week now, and i cant seem to find out how to fix it whenever i try to compile a c program, the linker
🌐
NI
knowledge.ni.com › KnowledgeArticleDetails
Undefined Symbol Error When Compiling LabWindows™/CVI Project - NI
December 23, 2023 - I am trying to compile a LabWindows™/CVI project, but I am getting errors that are referenced as Link Error or Undefined Symbol errors. error:Undefined symbol "symbol_name" referenced in "path"
🌐
Quora
quora.com › What-is-the-maths-symbol-for-undefined
What is the maths symbol for undefined? - Quora
Answer (1 of 6): There is no generally accepted symbol for "undefined." In math, this term refers to a value that is not assigned to any specific number. For example, if you try to calculate the value of pi using an infinite series, you will ...
🌐
Wikipedia
en.wikipedia.org › wiki › Undefined_(mathematics)
Undefined (mathematics) - Wikipedia
November 4, 2025 - In some mathematical contexts, undefined can refer to a primitive notion which is not defined in terms of simpler concepts. For example, in Elements, Euclid defines a point merely as "that of which there is no part", and a line merely as "length without breadth".
🌐
Texas Instruments E2E
e2e.ti.com › support › tools › code-composer-studio-group › ccs › f › code-composer-studio-forum › 250748 › undefined-symbol-unresolved-symbol
undefined symbol / unresolved symbol
March 9, 2013 - I have made progress but still have trouble. I linked TI driverlib.lib library to my project. This corrected the unresolved symbols that came from TI libraries. I am still having "unresolved symbols" error #10234-D my own symbols that in my own header files.
🌐
LabEx
labex.io › tutorials › cpp-how-to-resolve-undefined-symbol-errors-419008
How to resolve undefined symbol errors | LabEx
Undefined symbol errors are common ... and linking process. These errors occur when the compiler cannot find the implementation of a function or variable that is being used in your code....
🌐
LArSoft
larsoft.github.io › LArSoftWiki › Finding_unresolved_symbols.html
Finding Unresolved Symbols | LArSoft
How often have you encountered an error such as: undefined reference to `typeinfo for simb::MCParticle'? Where do you start looking for this? There are hundreds of libraries, it could be in any one of them. Here’s a solution: setup the larutils UPS product ( > v1_20_08 ) (or down load the script below) and use: find_global_symbol.sh [-d] [-f] [-a] [-u] [-v] [-t] [-h] <symbol>
🌐
Sysprogs
sysprogs.com › w › forums › topic › strange-undefined-symbols
Topic: strange undefined symbols | Sysprogs
January 4, 2022 - Error: L6218E: Undefined symbol __rt_locale (referred from _printf_fp_dec.o).