Recent Posts

Showing posts with label undefined. Show all posts
Showing posts with label undefined. Show all posts

A Comprehensive Guide to Unresolved External Symbols in C++

 Consider you have this piece of code:

 

 

During the compilation of b.cpp, the compiler treats the get() symbol as an external reference, deferring its resolution. The linker is subsequently responsible for locating the symbol's definition and unifying the object files generated 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".

 In accordance with C++ Standard terminology, the compilation process is partitioned into various translation phases. The concluding phase is the one that directly applies here:

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. 

 These errors surface during the final stage of the build process, known as linking. At this point, the compiler has already turned your individual source files into object files or libraries; the linker’s job is to "stitch" them together so they can function as a single program.

 Linking in the Wild: Visual Studio & Beyond

In Microsoft Visual Studio, projects typically produce .lib files. Think of these as catalogs containing two specific lists:

  • Exported symbols: The functions or variables this library provides to others.

  • Imported symbols: The "missing pieces" this library needs from elsewhere to work.

The linker looks at these tables and matches the imports of one file to the exports of another. While the terminology might change slightly, this same fundamental "handshake" happens across almost all compilers and platforms.

 Common Linker Error Messages

If the linker can't find a definition for one of those imported symbols, it will throw an error. Here is what that looks like depending on your environment: 

EnvironmentTypical Error Codes / Messages
MS Visual Studioerror LNK2001, error LNK2019, error LNK1120
GCC / Clangundefined reference to 'symbolName'

Essentially, the compiler is saying: "I know you want to use this function, but I can't find the actual code for it anywhere." 


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:


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

How to solve Undefined variable: _SESSION PHP

If you have this error then you must to call to session_start in the begining (at the top) of each php file where you want to use sessions

How to solve: Notice: Use of undefined constant XXX - assumed 'XXX'

This notice occurs when a token is used in the code and appears to be a constant, but a constant by that name is not defined.
One of the most common causes of this notice is failure to quote a string used as an associative array key.
For example:
// Wrong
echo $array[key];

// Right
echo $array['key'];
Another common causes is a missing $ (dollar) sign in front of a variable name:
// Wrong
echo varName;

// Right
echo $varName;
It can also be a sign that a needed PHP extension or library is missing when you try to access a constant defined by that library.

How to solve: Notice: Undefined variable

Happens when you try to use a variable that wasn't previously defined.
A typical example would be
foreach ($items as $item) {
    // do something with item
    $counter++;
}
If you didn't define $counter before, the code above will trigger the notice.
The correct way would be to set the variable before using it, even if it's just an empty string like
$counter = 0;
foreach ($items as $item) {
    // do something with item
    $counter++;
}

How to solve: Fatal error: Call to undefined function

This happens when you try to call a function that is not defined yet. Common causes include missing extensions and includes, conditional function declaration, function in function declaration or simple typos.
Example 1 - Conditional Function Declaration
$someCondition = false;
if ($someCondition === true) {
    function fn() {
        return 1;
    }
}
echo fn(); // triggers error
In this case, fn() will never be declared because $someCondition is not true.
Example 2 - Function in Function Declaration
function createFn() 
{
    function fn() {
        return 1;
    }
}
echo fn(); // triggers error
In this case, fn will only be declared once createFn() gets called. Note that subsequent calls to createFn() will trigger an error about Redeclaration of an Existing function.
In case of a missing extension, install that extension and enable it in php.ini. Refer to the Installation Instructions in the PHP Manual for the extension your function appears in.
In case of missing includes, make sure to include the file declaring the function before calling the function.
In case of typos, fix the typo.

How to solve: Notice: Undefined Index

Happens when you try to access an array by a key that does not exist in the array.
A typical example for an Undefined Index notice would be (demo)
$data = array('foo' => '42', 'bar');
echo $data['spinach'];
echo $data[1];
Both spinach and 1 do not exist in the array, causing an E_NOTICE to be triggered.
The solution is to make sure the index or offset does exist, either by using array_key_exists or isset prior to accessing that index.
$data = array('foo' => '42', 'bar');
if (array_key_exists('spinach', $data)) {
    echo $data['spinach'];
}
else {
    echo 'No key spinach in array';
}