0

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

[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() {
}
4

1 回答 1

6

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

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

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

于 2013-06-13T11:03:04.847 回答