Sophie Sperner
Sophie Sperner

Reputation: 4546

Undefined reference in C++ simple project

I'm trying to compile not mine project. I get this error:

[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

Main program is below (I reduced the code to the minimum possible case):

#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 file:

#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 file:

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

Upvotes: 0

Views: 199

Answers (1)

Mats Petersson
Mats Petersson

Reputation: 129364

You need to compile in "MinitAlgo.cpp" as well:

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

[Added -Wall as well, to give warnings for "all" things, which is typically a good thing, as nearly all warnings (aside from those added with -Wextra in some instances) are usually really errors]

Upvotes: 6

Related Questions