c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

标签 c++ linker-errors undefined-reference c++-faq unresolved-external

什么是 undefined reference / Unresolved external symbol 错误?什么是常见原因以及如何修复/预防它们?

最佳答案

正如 所指定的,编译 C++ 程序分几个步骤进行。 2.2 (credits to Keith Thompson for the reference) :

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

  1. Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters for end-of-line indicators) if necessary. [SNIP]
  2. Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. [SNIP]
  3. The source file is decomposed into preprocessing tokens (2.5) and sequences of white-space characters (including comments). [SNIP]
  4. Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed. [SNIP]
  5. Each source character set member in a character literal or a string literal, as well as each escape sequence and universal-character-name in a character literal or a non-raw string literal, is converted to the corresponding member of the execution character set; [SNIP]
  6. Adjacent string literal tokens are concatenated.
  7. White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. (2.7). The resulting tokens are syntactically and semantically analyzed and translated as a translation unit. [SNIP]
  8. Translated translation units and instantiation units are combined as follows: [SNIP]
  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. (emphasis mine)

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


指定的错误发生在编译的最后阶段,通常称为链接。这基本上意味着你将一堆实现文件编译成目标文件或库,现在你想让它们一起工作。
假设您定义了符号 aa.cpp .现在,b.cpp声明该符号并使用它。在链接之前,它只是假设该符号是在某处定义的,但它并不关心在哪里。链接阶段负责查找符号并将其正确链接到 b.cpp (好吧,实际上是针对使用它的对象或库)。
如果您使用的是 Microsoft Visual Studio,您将看到项目生成 .lib文件。它们包含一个导出符号表和一个导入符号表。导入的符号会根据您链接的库进行解析,并为使用该 .lib 的库提供导出的符号。 (如果有的话)。
其他编译器/平台也存在类似的机制。
常见的错误信息是 error LNK2001 , error LNK1120 , error LNK2019 Microsoft Visual Studio undefined reference to 的符号名称海合会 .
编码:
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;
}
将产生以下错误 海合会 :
/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
和类似的错误 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
常见原因包括:
  • 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)
  • 关于c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65441069/

    相关文章:

    c++ - 我正在尝试在C++上做傻瓜练习,但遇到错误,说 'GraduateStudent'类没有名为 'advisor'的任何字段

    python - 将代码从 Python 翻译为 C++

    c++ - 运算符优先级与评估顺序

    html - 如何链接到 css 中的 div 并使 <p> 具有设定的宽度?

    c++ - 当所有信息都在头文件中时未定义的函数引用

    c++ - 整数年;即将出现 -2 但我还没有给它赋值

    c++ - MacOSX 的 Opencv Xcode 链接器错误

    c - 错误: ld returned 1 exit status at C

    C调用函数类型错误

    c - 未定义对 `initscr' Ncurses 的引用