0

i am learning to implement a simple Stack base machine using Linklist with c++. There are constant.h, Stackframe.h, StackFrame,cpp, main.cpp. I code on MacOs 10.14.6 with Visual Studio Code. I am getting some trouble when i run main.cpp file. Here is error that i got:

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)

My code is below.

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;
}

Hope you guys will help me. (Sorry for my bad English)

Lang Ung
  • 5
  • 2

1 Answers1

0

You haven't told g++ to compile and link in StackFrame.cpp.

Where you have

$ g++ main.cpp -o main

you should have

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

The addition of -lstdc++ might not be required with newer versions of g++.

James McPherson
  • 2,476
  • 1
  • 12
  • 16