V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
newton108
V2EX  ›  编程

C++新手求助!!

  •  
  •   newton108 · 2016-02-16 12:03:26 +08:00 · 2183 次点击
    这是一个创建于 3211 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一共 5 个文件在同一文件夹。
    - main.cpp
    - genlib.h
    - genlib.cpp
    - simpio.h
    - simpio.cpp

    可是编译的时候说找不到 GetInteger(),可是我明明在 simpio.cpp 里定义了啊。

    $ g++ main.cpp
    Undefined symbols for architecture x86_64:
    "GetInteger()", referenced from:
    _main in main-32d6b5.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    文件如下。

    /*
    * File: mian.cpp
    * --------------
    */
    
    #include <iostream>
    #include "genlib.h"
    #include "simpio.h"
    
    int main()
    {
        int total;
        total = GetInteger();
        cout << "The total is " << total << "." << endl;
        return 0;
    }
    
    #ifndef GENLIB_H_
    #define GENLIB_H_
    
    /*
     * File: genlib.h
     * Version: 1.0CPP
     * Last modified on Wed Sep 18 13:41:31 PDT 2002 by jzelenski
     * ----------------------------------------------------------
     * This header file is indicated to be included in
     * all the programs written for CS106B/X and provides a few
     * common definitions. Note this header has a "using namespace std"
     * clause. If a file includes this header, it can then use
     * features from the std namespace without qualifying by scope.
     */
    
    #include <string>
    using namespace std;
    
    /*
     * Function: Error
     * Usage: Error(msg)
     * ------------------
     * Error outputs an error string to the cerr stream and
     * then throws a string exception corresponding to the error.
     */
    
    void Error(string str);
    
    
    // /*
    //  * Function macro: main
    //  * --------------------
    //  * The purpose of this macro definition is to rename the student
    //  * main to Main in order to allow a custom main defined in our
    //  * libraries to configure the application before passing control
    //  * back to the student program.
    //  */
    // #ifdef __APPLE__
    // #define main Main
    // #endif
    
    
    #endif /*GENLIB_H_*/
    
    /*
     * File: genlib.cpp
     * Last modified September 2011 by Colin
     * ----------------
     * This file implements the genlib.h interface
     * It is needed by programs using the Stanford CS106B libraries
     * If the ErrorException is unhandled by student code, it will
     * be caught by init.cpp code, which wraps around the student's
     * main() function
     */
    
    #include "genlib.h"
    #include <cstdlib>
    #include <iostream>
    
    ErrorException::ErrorException(string m="unspecified custom error")
    : msg(m) {
    }
    
    ErrorException::~ErrorException() throw() {}
    
    const char* ErrorException::what() const throw() {
        return this->msg.c_str();
    }
    
    void Error(string str) {
        ErrorException err(str);
        throw err;
    }
    
    #ifndef SIMPIO_H_
    #define SIMPIO_H_
    /*
     * File: simpio.h
     * Version: 1.0CPP
     * Last modified on Wed Sep 18 13:34:29 PDT 2002 by jzelenski
     * ----------------------------------------------------------
     * This interface provides access to a simple package of
     * functions that simplify the reading of console input.
     */
    
    #include "genlib.h"
    
    /*
     * Function: GetInteger
     * Usage: n = GetInteger();
     * ------------------------
     * GetInteger reads a line of text from standard input and scans
     * it as an integer.  The integer value is returned.  If an
     * integer cannot be scanned or if more characters follow the
     * number, the user is given a chance to retry.
     */
    
    int GetInteger();
    
    
    /*
     * Function: GetLong
     * Usage: n = GetLong();
     * ---------------------
     * GetLong reads a line of text from standard input and scans
     * it into a long integer.  The long is returned.  If the 
     * number cannot be scanned or if extra characters follow it,
     * the user is given a chance to retry.
     */
    
    long GetLong();
    
    /*
     * Function: GetReal
     * Usage: x = GetReal();
     * ---------------------
     * GetReal reads a line of text from standard input and scans
     * it as a double.  If the number cannot be scanned or if extra
     * characters follow after the number ends, the user is given
     * a chance to reenter the value.
     */
    
    double GetReal();
    
    
    /*
     * Function: GetLine
     * Usage: s = GetLine();
     * ---------------------
     * GetLine reads a line of text from standard input and returns
     * the line as a string.  The newline character that terminates
     * the input is not stored as part of the string that is returned.
     */
    
    string GetLine();
    
    #endif /*SIMPIO_H_*/
    
    /* File : simpio.cpp
     * Last modified September 2011 by Colin
     *
     * Based on the discussion in the CS106L Course Reader, chapter 3
     * http://www.stanford.edu/class/cs106l/course_reader.html
     */
    
    #include <iostream>
    #include <sstream>
    #include "genlib.h"
    
    
    /*
     * Function: GetLine
     * Usage: s = GetLine();
     */
    
    string GetLine() {
      string result;
    
        getline(cin, result);
        if (cin.fail()) {
            result = "";
            cin.clear();
        }
    
        return result;
    }
    
    /*
     * Function: GetInteger
     * Usage: n = GetInteger();
     */
    
    int GetInteger() {
        while(true) {
        stringstream converter;
        converter << GetLine();
    
        int result;
        if (converter >> result) {
            char remaining;
            if(converter >> remaining)
            cout << "Unexpected character: " << remaining << endl;
            else
            return result;
        } else
            cout << "Please enter an integer." << endl;
        }
    }
    
    
    /*
     * Function: GetLong
     * Usage: n = GetLong();
     */
    
    long GetLong() {
        while(true) {
        stringstream converter;
        converter << GetLine();
    
        long result;
        if(converter >> result) {
            char remaining;
            if(converter >> remaining)
            cout << "Unexpected character: " << remaining << endl;
            else
            return result;
        }
        else
            cout << "Please enter an integer (long)." << endl;
       }
    }
    
    /*
     * Function: GetReal
     * Usage: x = GetReal();
     */
    
    double GetReal() {
        while(true) {
        stringstream converter;
        converter << GetLine();
    
        double result;
        if (converter >> result) {
            char remaining;
            if (converter >> remaining)
            cout << "Unexpected character: " << remaining << endl;
            else
            return result;
        }
        else
            cout << "Please enter a floating-point number.";
        }
    }
    
    3 条回复    2016-02-16 12:50:53 +08:00
    omengye
        1
    omengye  
       2016-02-16 12:39:12 +08:00 via Android   ❤️ 1
    关键词: g++ 多个源文件
    newton108
        2
    newton108  
    OP
       2016-02-16 12:46:56 +08:00
    @omengye 谢谢,已搞定了。
    matthewgao
        3
    matthewgao  
       2016-02-16 12:50:53 +08:00   ❤️ 1
    原因是你只编译了 main.cpp ,但是没编译其他的源文件,所以在连接的时候找不到 getInteger
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2838 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:26 · PVG 20:26 · LAX 04:26 · JFK 07:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.