仅使用.cpp

时间:2015-12-02 11:39:59

标签: c++ templates compilation undefined-reference

我是C ++初学者,很抱歉这个问题很愚蠢。

我有三个文件:main.cppmapper.hpp及其src文件mapper.cpp

mapper.hpp声明一个扩展ff_node的结构,它具有纯虚方法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_ */

这是mapper.cpp

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

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

最后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>>());
//..
}

使用以下命令进行编译:

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

返回以下错误:

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

奇怪的是,如果我声明svc(void *)内幕mapper.hpp,则不会返回任何错误,并且所有内容都已正确编译!

0 个答案:

没有答案