跟我一起来学cuda---解决error C2065: “blockIdx”: 未声明的标识符

本文档记录了作者在学习CUDA过程中遇到的'blockIdx'未声明错误的解决步骤,包括新建Win32空项目,设置CUDA C/C++编译器,配置附加库目录,链接器设置,以及解决.cpp文件编译问题。通过这些步骤,成功解决问题并能在调试窗口查看结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于毕业需要,需要cuda来写毕业论文。楼主小白一枚,跟着开勇的博客开始学习。博客详细地址请参考:http://blog.youkuaiyun.com/openhero/article/details/42131771

《GPU的革命》


首先新建一个win32空项目,进行如下设置:

1、右键源文件文件夹->新建项->选择cuda c/c++->新建一个以.cu结尾的文件
2、右键工程-》生成自定义-》选择cuda生成
3、右键test.cu-》属性-》选择cuda c/c++编译器
4、右键工程-》属性-》链接器-》常规-》附加库目录-》添加目录 $(CUDA_PATH_V5_5)\lib\$(Platform);
5、在链接器-》输入中添加 cudart.lib

6、在工具-》选项-》文本编辑器-》文件扩展名-》添加cu \cuh两个文件扩展名


设置完毕后开始输入代码

#include<stdio.h>
#include<iostream>
#include<cuda_runtime.h>
using namespace std;
/*=============================================================*/
/*************************thread demo***************************/
/*=============================================================*/

#define BLOCK_DIM 512

const int size_x = 512;
const int size_y = 1;

__global__ static void ThreadDemo1(unsigned int *ret)
{
	unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
	unsigned int yIndex = blockDim.y * blockIdx.y + threadIdx.y;

	if(xIndex < size_x && yIndex < size_y)
	{
		unsigned int index = xIndex + size_x * yIndex;
		ret[index] = xIndex;
		ret[index + size_x * size_y] = yIndex;
	}

}

int main()
{
	unsigned int *ret = 0;
	unsigned int host_ret[size_x * size_y * 2] = {0};
	int i = 0;
	cudaMalloc((void**)&ret, sizeof(unsigned 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值