I got a error ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1

I got a error ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1

我正在学习使用 Linklist 和 C++ 实现一个简单的 Stack 基础机器。有constant.h、Stackframe.h、StackFrame、cpp、main.cpp。我使用 Visual Studio 代码在 MacOs 10.14.6 上编写代码。 当我 运行 main.cpp 文件时,我遇到了一些麻烦。 这是我得到的错误:

cd "/Volumes/public/initial/" && g++ main.cpp -o main && 
"/Volumes/public/initial/"main
[MACs-MacBook-Pro:CTDL/BTL/stack-machine-master] macosx% cd 
"/Volumes/public/initial/" && g++ main.cpp -o main && "/Volumes/public/initial/"main
Undefined symbols for architecture x86_64:
"StackFrame::run(std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> >)", referenced from:
test(std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> >) in main-403bc1.o
"StackFrame::StackFrame()", referenced from:
test(std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> >) in main-403bc1.o
"StackFrame::~StackFrame()", referenced from:
test(std::__1::basic_string<char, std::__1::char_traits<char>, 
std::__1::allocator<char> >) in main-403bc1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我的代码如下。

constant.h:

#ifndef __JAVM_CONSTANTS_H__
#define __JAVM_CONSTANTS_H__

#define OPERAND_STACK_MAX_SIZE 32 
#define LOCAL_VARIABLE_ARRAY_SIZE 256

#endif // !__JAVM_CONSTANTS_H__

StackFrame.h

#ifndef __STACK_FRAME_H__
#define __STACK_FRAME_H__

#include <string>
/*
StackFrame declaration
*/
class StackFrame {
    int opStackMaxSize; // max size of operand stack
    int localVarArrSize; // size of local variable array
public:
    /*
    Constructor of StackFrame
    */
    StackFrame();
    /*
    Destructor of StackFrame
    */
    ~StackFrame();
    /*
    Run the method written in the testcase
    @param filename name of the file
    */
    void run(std::string filename);
    //Instruction
};

#endif // !__STACK_FRAME_H__

StackFrame.cpp

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

StackFrame::StackFrame() : opStackMaxSize(OPERAND_STACK_MAX_SIZE), 
localVarArrSize(LOCAL_VARIABLE_ARRAY_SIZE) {} 

StackFrame::~StackFrame() {}

void StackFrame::run(string filename)
{
    cout<<filename<<endl;
}

main.cpp

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

/*
Run the testcase written in `filename`
@param filename name of the file
*/
void test(string filename) {
    StackFrame *sf = new StackFrame();
    try {
        sf->run(filename);
    }
    catch (exception& e) {
        cout << e.what();
    }
    delete sf;
}

/*
Main function
*/
int main() {
    string s = "test000.txt";
    test(s);
    return 0;
}

希望大家帮帮我。 (抱歉我的英语不好)

你还没有告诉 g++StackFrame.cpp 中编译和 link。

你在哪里

$ g++ main.cpp -o main

你应该

$ g++ main.cpp StackFrame.cpp -o main -lstdc++

新版本的 g++ 可能不需要添加 -lstdc++