Visual Studio中编译.cu +.cpp文件报错:C++ 未定义标识符 “clock64“....

Visual Studio中编译.cu文件报错

问题1:C++ 未定义标识符 “clock64”

右键.cu文件选择属性/Configuration Properties, 转到 CUDA C/C++DeviceCode Generation,然后检查它的设置:compute_X,sm_X (X数字的选择看https://arnon.dk/matching-sm-architectures-arch-and-gencode-for-various-nvidia-cards/)
在这里插入图片描述像我的显卡是RTX3060,所以我需要将compute_52,sm_52改成compute_86,sm_86
在这里插入图片描述然后右键你的项目,选择属性/Configuration Properties, 转到 CUDA C/C++DeviceCode Generationcompute_52,sm_52也要修改成compute_86,sm_86
在这里插入图片描述如果还是报错,尝试以下两种方式:

  1. 增加头文件# include <time.h> 或者sm_20_intrinsics.h
  2. clock64()修改成clock()

问题2:未定义标识符 “blockIdx”

  1. 增加头文件#inlcude<device_launch_parameters_h>

  2. 修改.cu文件的配置属性,把项类型修改为CUDA C/C++(右键.cu文件,选择属性;其他缺少未定义标识符时,也要看一下这个改了没改)
    在这里插入图片描述cite: https://blog.csdn.net/weixin_42478379/article/details/103656585

问题3:未定义标识符 “rsqrtf”

增加头文件#include <math_functions.h>

问题4:C++ 未定义标识符 “tex2D”

同问题11

  1. 增加头文件#include "cpu_anim.h" (cite:https://blog.csdn.net/xiao_lxl/article/details/52777670; 下载链接:https://github.com/tpoisot/CUDA-training/tree/master/utils/cuda_by_example/common) - 我试过了,没用
  2. 还有说 tex2D被弃用了,(cite:https://stackoverflow.com/questions/67193791/how-to-replace-the-deprecated-tex2dtexturet-2-cudareadmodeelementtype-floa

问题5: E0266 “ACCESS_MASK” 不明确

这个错误的原因是:opencv的using namespace cv和windows.h中ACCESS_MASK定义冲突
注:养成写代码不用using namespace …的习惯

  1. 去掉using namespace cv,用cv::代替

问题6: E0035 #error 指令: – incorrect inclusion of a cudart header file

检查头文件的位置,一般写在.h文件中,然后在.cpp中只包含一个.h文件
#include <...>尽量不要重复出现在多个文件中
然后用以下代码替换掉#include <math_functions.h>

#define __CUDA_INTERNAL_COMPILATION__
#include "math_functions.h"
#undef __CUDA_INTERNAL_COMPILATION__

cite:
https://stackoverflow.com/questions/16035401/incorrect-inclusion-of-a-cudart-header-error-on-cuda
https://www.codenong.com/16035401/
https://blog.csdn.net/tiao_god/article/details/112463732

问题7: 未定义标识符 “CudaSafeCall”

make your own wrapper
自己写函数CudaSafeCall
cite:
https://forums.developer.nvidia.com/t/is-cudasafecall-no-longer-needed/27606
https://www.cnblogs.com/catnip/p/8630046.html
https://vimsky.com/examples/detail/cpp-ex-----CUDA_SAFE_CALL-function.html

问题8: E1097 未知特性 “no_init_all”

出现在winnt.h文件中
winnt.h加入#define no_init_all deprecated
在这里插入图片描述 cite:
https://www.coder.work/article/7473663
https://blog.csdn.net/weixin_44749766/article/details/90147106

问题9: 错误C4996 ‘fopen’:

报错:
‘fopen’: This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
解决办法:
https://blog.csdn.net/muzihuaner/article/details/109886974

_CRT_SECURE_NO_WARNINGS

在这里插入图片描述

问题10: 错误C2338 wrong character type for setfill

setfill("0") 改成setfill('0')

问题11:在核函数<<<处提示:应输入表达式(但是可以编译成功)

vs的报错并没有影响工程的顺利编译,这个错误可能仅仅是vs对cuda语法的识别问题,就像对cuda中 “ <<< >>> ” 的不识别一样。(cite: https://www.jianshu.com/p/ed486942647b
或:

  1. 加external “C” (我尝试了,没有用; cite:https://www.jianshu.com/p/7b40b7f846d7;https://blog.csdn.net/qq_35789421/article/details/117398582
  2. 全局变量问题: https://blog.csdn.net/xdearluo/article/details/84142677
  3. 代码错误问题:https://blog.csdn.net/xdearluo/article/details/84142677

参考链接:
4. https://www.codenong.com/11217117/
5. cuda出现“error:未识别的blockIdx”解决方案: https://blog.csdn.net/deyuzhi/article/details/44114717
6. error:“ACCESS_MASK”: 不明确的符号:https://blog.csdn.net/jiao_mrswang/article/details/100209833
7. vs+cuda的环境配置:https://blog.csdn.net/weixin_41336841/article/details/118313452
8. .cpp 调用CUDA的.cu中的函数:https://www.cnblogs.com/betterwgo/p/6843272.html

猜你喜欢

转载自blog.csdn.net/weixin_43686259/article/details/129771445
今日推荐