如果我尝试在同一项目中使用 stl 矢量和 CUDA 推力矢量,为什么会出现链接错误
Why there is linking error if I try to use stl vector and CUDA thrust vector in the same project?
如果我尝试在同一项目中使用 stl 矢量和 CUDA 推力矢量,为什么会出现链接错误?
文件1.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
谁能给我任何想法?
最有可能的问题在
using namespace std;
使用它根本不是好的做法,但如果您选择使用它,至少将其放入使用它的.cpp文件中,而不是放在头文件中。通过将其放在头文件中,您可以将其推断到包括该头文件在内的所有文件。
相关文章:
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 为什么 CUDA 不会导致C++代码加速?
- 为什么以下 POP 功能无法在主机或设备 (CUDA) 上运行?
- 为什么cuda设备计数为零
- 为什么 cuda-gdb 会启动多个线程?
- CUDA 内核在第二次运行时运行得更快 - 为什么?
- 为什么 CUDA 在访问类成员时崩溃?
- 为什么 CUDA .cu 文件中会出现多个定义错误
- 为什么我在 CUDA 中实施总和减少时得到错误的结果?
- 为什么在循环中重复一个内核会使CUDA代码明显较慢
- 为什么CUDA共享内存比瓷砖矩阵乘法中的全局内存慢
- 为什么CMAKE add_依赖项不适用于使用CUDA代码的库
- 为什么我的数据不适合CUDA纹理对象
- 为什么我的 CUDA 程序的初始执行比后续执行花费更长的时间
- 为什么调用 CUDA 内核函数时这个类成员变量没有改变
- C++和CUDA:为什么代码每次返回不同的结果
- 为什么AddVector CUDA C 不起作用
- 为什么我的CUDA实现和我的CPU实现一样快
- 为什么我的CUDA内核返回旧值
- CUDA:为什么不能定义静态全局成员函数