mercredi 2 décembre 2015

undefined reference only with .cpp

I'm a C++ beginner, so sorry if this question is stupid.

I have three file: main.cpp, mapper.hpp and its src file mapper.cpp.

mapper.hpp declare a struct which extends ff_node, which has the pure virtual method void* svc(void* task);:

//mapper.hpp
#define HAS CXX11 VARIADIC TEMPLATES 1

#include <iostream>
#include <ff/pipeline.hpp>

using namespace std;

#ifndef MAPPER_HPP_
#define MAPPER_HPP_

template<typename S1>
struct Mapper : ff::ff_node {
public:
    void* svc(void* task);
};

#endif /* MAPPER_HPP_ */

This is mapper.cpp:

//mapper.cpp
#include "mapper.hpp"

template<typename S1>
void* Mapper<S1>::svc(void* task) {
    //do something here
}

And finally main.cpp:

#include <iostream>
#include <vector>
#include <ff/pipeline.hpp>
#include <ff/farm.hpp>

#include "mapper.hpp"
#include "splitter.hpp"

using namespace ff;

int main(int argc, char *argv[]) {
//...
    std::vector<std::unique_ptr<ff_node> > Workers;
    for(int i=0;i<nworkers;++i)
        Workers.push_back(make_unique<Mapper<int>>());
//..
}

Compiling with the following command:

g++ -std=c++11 -DNO_DEFAULT_MAPPING -I /home/luca/fastflow -O3 -finline-functions -DNDEBUG mapper.cpp Main.cpp -o Main -pthread

Returns me the following error:

/tmp/ccUY3VtR.o:(.rodata._ZTV6MapperIiE[_ZTV6MapperIiE]+0x118): undefined reference to `Mapper<int>::svc(void*)'
collect2: error: ld returned 1 exit status

What is strange is that if I declare svc(void*) insider mapper.hpp then no error is returned and everything is compiled correctly!

Aucun commentaire:

Enregistrer un commentaire