c++ - 体系结构 x86_64 : Compiling in C++ 的 undefined symbol

标签 c++ x86

在使用链编写 C++ 程序时遇到编译问题却不知道为什么。

我得到的错误是:

Undefined symbols for architecture x86_64:
  "userInputOutput(linearList*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
      _main in ccBEoeAc.o
  "chain::chain(int)", referenced from:
      _main in ccBEoeAc.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

如果我使用 g++ -c main2.cpp,它会编译,但我想确保我不只是用那个选项来掩盖某些事情。

主2.cpp

#include <iostream>
#include "Chain.h"
#include "IOcode.h"

using namespace std;

int main (){

    //Call constructor, initial capacity 10
    chain *myChain=new chain(10);

    //Print initial chain
    userInputOutput(myChain, "chain");
}

链.cpp

 #include "Chain.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include "Myexception.h"

using namespace std;

    chain::chain(int initialCapacity)
    {
    cout<<"This method is working"<<endl;
/*  if (initialCapacity <1)
    {
        //throw illegalParameterValue();
    }
    firstNode=NULL;
    listSize=0;
*/
};

IOcode.cpp

#include <iostream>
#include "Linearlist.h"

using namespace std;

void userInputOutput (linearList* l, string dataStructure){

    for (int i = 0; i < 13; i++)
            l->insert(i, i+1);
    cout<<"The initial "<<dataStructure<<" is: ";
    l->traverse();
    cout<<"welcome to the assignmet 3!"<<endl;
    while(true){
       //do stuff

    }
}

知道我为什么会收到这些错误吗? IOcode 文件已提供给我,即我创建的 chain.cpp。对于糟糕的代码,C++ 的新手,我们深表歉意。

最佳答案

你只是在编译一个文件。

这对于快速而肮脏的尝试应该没问题:

g++  main2.cpp Chain.cpp IOcode.cpp 

尽管我会添加 -Wall -Wextra -pedantic -Werror

关于c++ - 体系结构 x86_64 : Compiling in C++ 的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19059519/

相关文章:

.net-4.0 - 为什么我的网站需要 "Enable 32-bit applications"?

assembly - x86 程序集 - 为什么 [e]bx 保留在调用约定中?

assembly - 如何在 i386 上将 "pushl 2000"从 AT&T asm 转换为 Intel 语法

c++ - 通过其构造函数将依赖项传递给包装器对象

c++ - 返回类型模板(enable_if)禁止什么?

c++ - Webots ROS默认 Controller "RosInertialUnit.cpp"中的四元数计算

assembly - 汇编中的标签如何知道它的运行时地址?

c - 在 x86 Assembly 中组装代码时出错

c++ - 如何在 C++ 中启用共享打开的文件?

c++ - 在概念约束中使用变量模板会产生编译错误