【问题标题】:LNK2019 error c++ unresolved external symbolLNK2019 错误 c++ 无法解析的外部符号
【发布时间】:2011-08-09 11:08:16
【问题描述】:

我收到了这些错误消息:

错误 1 ​​错误 LNK2019:未解决 外部符号“公共:无效 __thiscall ArrayIntStorage::sortOwn(void)" (?sortOwn@ArrayIntStorage@@QAEXXZ) 在函数中引用 _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

错误 2 错误 LNK2019:未解决 外部符号“公共:无效 __thiscall ArrayIntStorage::sortStd(void)" (?sortStd@ArrayIntStorage@@QAEXXZ) 在函数中引用 _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

错误 3 错误 LNK2019:未解决 外部符号“类 std::basic_ostream > & __cdecl 运算符 &,类 ArrayIntStorage 常量 &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVArrayIntStorage@@@Z) 在函数中引用 _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

错误 4 错误 LNK2019:未解决 外部符号“类 std::basic_istream > & __cdecl 运算符>>(类 std::basic_istream > &,类 ArrayIntStorage &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVArrayIntStorage@@@Z) 在函数中引用 _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

错误 5 错误 LNK2019:未解决 外部符号“公共:布尔 __thiscall ArrayIntStorage::setReadSort(bool)" (?setReadSort@ArrayIntStorage@@QAE_N_N@Z) 在函数中引用 _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

错误 6 错误 LNK1120: 5 未解决 外部 G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\Debug\C_Style_Array.exe 1 1 C_Style_Array

我不知道发生了什么,我想知道我是否错过了什么? 我是新手,它没有给我任何行号,所以我不确定要给你哪个代码,所以我会给你这部分

#include <fstream>
#include <iostream>
using namespace std;
#include "ArrayIntStorage.h"

int main(int argc, char **argv) {

ifstream fin1("ACW2_data.txt");
ofstream out1("1-arrayUnsortedRead.txt");
ofstream out2("2-arrayUnsortedRead-thenSTDSort.txt");

if(!fin1.is_open()) 
{
    cout << "FAIL" << endl;
    return 1;
}

ArrayIntStorage arrayStorage1;
arrayStorage1.setReadSort(false);   // do not read sort

// read in int values into data structure
fin1 >> arrayStorage1;

// output int values in data structure to file
out1 << arrayStorage1;

// sort data structure using std
arrayStorage1.sortStd();

// output int values in data structure to file
out2 << arrayStorage1;

fin1.close();
out1.close();
out2.close();

【问题讨论】:

    标签: c++


    【解决方案1】:

    您的链接器(编译器的一部分)找不到ArrayIntStorage::sortOwn() 的定义位置。

    这通常会发生:

    1. ArrayIntStorage::sortOwn() 的定义在另一个 .c 文件中,您忘记告诉编译器(因此没有编译),
    2. ArrayIntStorage 是一个只有头文件的库(因此没有其他 .c 文件),在这种情况下,您可能忘记实现函数 sortOwn(),而只是声明了它。
    3. ArrayIntStorage 是一个尚未链接的外部库。 (如 Tomalak Geretkal 注释,并按照 paxdiablo 规定的步骤解决)

    如果两者都不是,或者您觉得这些选项令人困惑,请发布头文件ArrayIntStorage.h 和对应的.c 文件(应该有)。

    【讨论】:

      【解决方案2】:

      这是一个链接器错误,一旦您了解该过程就可以轻松解决。

      通过#include-ing 源代码中的头文件,让编译器知道它需要的定义。

      但是,还需要一个额外的步骤。您必须将所有不同的目标文件和库链接在一起。

      这是因为,虽然标头包含有关 ArrayIntStorage 内容的信息,但它的实际 代码 位于其他位置。这就是在链接阶段购买的东西。

      基本上,您需要确保目标文件或库包含在您的构建过程中。

      例如,以下 gcc 命令将包含 abc.o 对象模块并从 libxyz.a 归档库中引入所需的任何内容:

      gcc -o myprog myprog.c abc.o -L/path/to/libs -lxyz
      

      也可以针对不同的环境采取不同的方式。例如,IDE 很可能会将其置于某种项目设置下。

      This answer 提供了许多环境中常见的编译和链接过程的更多信息。

      【讨论】:

        【解决方案3】:

        您忘记链接定义 ArrayIntStorage 函数的库。阅读该库的文档,了解如何在您的项目中使用它。

        【讨论】:

          【解决方案4】:

          ArrayIntStorage 似乎被编译成一个库。检查项目的链接器详细信息,然后添加库。

          【讨论】:

            【解决方案5】:

            可能与此问题中的代码无关,但如果您忘记在库中导出函数声明,然后尝试从该库外部使用该函数,也会出现此错误。比如这段代码

            bool operator==(const Foo &a, const Foo &b);
            

            应该变成

            MY_EXPORT_MACRO bool operator==(const Foo &a, const Foo &b);
            

            【讨论】:

              猜你喜欢
              • 2020-07-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-07-10
              • 2018-07-04
              • 2013-06-15
              • 2015-01-18
              • 1970-01-01
              相关资源
              最近更新 更多