如果我尝试在同一个项目中使用stl向量和CUDA推力向量,为什么会出现链接错误?

时间:2013-12-20 18:57:04

标签: c++ stl cuda thrust

如果我尝试在同一个项目中使用stl向量和CUDA推力向量,为什么会出现链接错误?

File1.h

#include <vector>
using namespace std;
class A{
public:
    A();
    vector<int> vec;
//....
};

File2.cu

#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
void ComputeDer(){
thrust::device_vector<int> Dh(4);
thrust::host_vector<int> H(4);//only host_vector can compile.
}

如果我注释掉其中一个向量声明,则可以编译代码,但如果两者都存在,则会出现以下错误:

1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in ComputeDer.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in ComputeDer.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z) already defined in ComputeDer.cu.obj
1>msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: char const * __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::c_str(void)const " (?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ) already defined in ComputeDer.cu.obj

任何人都可以给我任何想法吗?

1 个答案:

答案 0 :(得分:1)

最有可能的问题在于

using namespace std;

根本不习惯使用它,但是如果你选择使用它,至少使用它将它放入.cpp文件中,但不能放在头文件中。通过将其放在头文件中,您可以将其外推到包括该头文件在内的所有文件。