c++ - 如果我尝试在同一个项目中使用 STL vector 和 CUDA 推力 vector ,为什么会出现链接错误?

标签 c++ stl cuda thrust

为什么我尝试在同一个项目中使用 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/

相关文章:

cuda - 在 GPU 上查找 FFT 的最快库是什么?

c++ - cuda 内核未针对所有 blockIdx 执行

c++ - 提高密集光流分析的性能(容易)?

c++ - 赋值运算符的参数类型(引用或值?)

c++ - 编程语言的解析器应该做什么?

c++ - 尽可能小地从 Boost 中提取 sublib

c++ - 在流中的下一行之后定位迭代器的最简单方法?

c++ - 如何判断 wchar_t 是否有代理项(UTF-16)?

c++ - 在Qt中,如何使用定时器来启用/禁用一个功能?

c++ - 我将如何使用 for_each 删除 STL 映射中的每个值?