Undefined Reference Compiler Error

后端 未结 2 1596
灰色年华
灰色年华 2021-01-07 01:00

I think I\'m getting close, but I\'m having this error I\'ve been banging my head against the wall on for hours. I\'m missing something stupid, and I\'ve gone character by c

2条回答
  •  有刺的猬
    2021-01-07 01:41

    The command line for g++ needs to include both source files, like this:

    g++ -g -Wall main.cpp translator.cpp -o Pear
    

    Otherwise, the compiler has no idea from where to get the implementation of the translator::translator(std::istream&) member function.

    *EDIT: * (from the comment)

    I thought that basically the use of header files was so that it would know where to get each implementation of the file?

    This part is grossly oversimplified, but it should help you get the picture. Recall that the process of producing an executable from C++ sources consists of two major steps - compilation and linking. The g++ program performs them both (it can do just one if you specify -c flags, or pass only .o files).

    The compiler and the linker stages of g++ do not "talk" to each other directly. The compiler produces the inputs for the linker, and that's where the communication ends.

    Header files are for the compiler. Specifically, they are for the first stage of compilation - the preprocessing. Once the preprocessor has finished, there is no knowledge of where the definitions came from. Even the compiler does not know it, let alone the linker. That is why you need to "help" the linker by supplying all the relevant sources to g++.

提交回复
热议问题