C ++简单项目中未定义的引用

时间:2013-06-13 10:59:49

标签: c++ compiler-errors undefined-reference

我正在尝试编译不是我的项目。我收到这个错误:

[sophie@laptop minit]$ g++ -o minit minit.cpp
/tmp/ccxr5oWl.o: In function `main':
minit.cpp:(.text+0x4e6): undefined reference to `minit::MinitAlgo::MinitAlgo(std::string)'
minit.cpp:(.text+0x66c): undefined reference to `minit::MinitAlgo::~MinitAlgo()'
minit.cpp:(.text+0x6af): undefined reference to `minit::MinitAlgo::~MinitAlgo()'
minit.cpp:(.text+0x6df): undefined reference to `minit::MinitAlgo::~MinitAlgo()'
collect2: error: ld returned 1 exit status

主要程序如下(我将代码缩减到最小可能的情况):

#include <iostream>
#include <sstream>
#include <string>
#include "MinitAlgo.h"

int main(int argc, char * argv[]) {
    std::string fileName = "/home/table.dat";
    minit::MinitAlgo m(fileName);
}

MinitAlgo.h档案:

#ifndef MINITALGO
#define MINITALGO

#include <string>
#include <vector>

namespace minit {

    class MinitAlgo {
        public:
           MinitAlgo(std::string filename);
           ~MinitAlgo();
        private:
           // settings
           bool showDataset;
           bool showRankItems;
           int logTime;
           int debugLevel;
           int countOnly;
    };

}
#endif

MinitAlgo.cpp档案:

#include "MinitAlgo.h"
#include <string>
#include <sstream>
#include <iterator>
using namespace minit;

MinitAlgo::MinitAlgo(std::string filename) {
   this->showDataset = false;
   this->showRankItems = false;
   this->logTime = 0;
   this->debugLevel = 0;
   this->countOnly = false;
}

MinitAlgo::~MinitAlgo() {
}

1 个答案:

答案 0 :(得分:6)

您还需要编译“MinitAlgo.cpp”:

g++ -Wall -o minit minit.cpp MinitAlgo.cpp

[也添加-Wall,为“所有”事情发出警告,这通常是一件好事,因为几乎所有警告(除了在某些情况下添加了-Wextra的警告)通常都是真的错误]