为什么我尝试在同一个项目中使用 STL vector 和 CUDA 推力 vector 时会出现链接错误?
文件1.h
#include <vector>
using namespace std;
class A{
public:
A();
vector<int> vec;
//....
};
文件2.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.
}
如果我注释掉其中一个 vector 声明,代码可以编译,但如果两者都存在,则会出现以下错误:
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
谁能告诉我任何想法?
最佳答案
最有可能的问题在
using namespace std;
使用它根本不是一个好习惯,但如果你选择使用它,至少将它放入 .cpp 文件中使用它而不是在你的头文件中。通过将它放在头文件中,您可以将其外推到包括该头文件的所有文件。
关于c++ - 如果我尝试在同一个项目中使用 STL vector 和 CUDA 推力 vector ,为什么会出现链接错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20710241/