error LNK2019 && fatal error LNK1120

wulonghua 2011-02-05 10:15:17
我是cuda初学者,照着GPU高性能运算之CUDA这本书编程,第二章的小程序编译运行出现了如下两个错误:
错误1 error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用 LIBCMT.lib
错误2 fatal error LNK1120: 1 个无法解析的外部命令 G:\CUDA\example_2\Debug\example_2.exe
源代码如下:
//example_2.cu
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cutil.h>
#include "example_2_kernel.cu"
__host__ void
runTest(int argc, char** argv)
{
CUT_DEVICE_INIT(argc,argv); //启动CUDA
unsigned int mem_size = sizeof(float)* 4*4;
float* h_idata;
CUDA_SAFE_CALL(cudaMallocHost((void**)&h_idata,mem_size));
for(unsigned int i = 0;i<4;i++)
for(unsigned int j = 0;j<4;j++)
h_idata[i*4+j]=1.0f;

float* d_idata;
CUDA_SAFE_CALL(cudaMalloc((void**)&d_idata,mem_size));

CUDA_SAFE_CALL(cudaMemcpy(d_idata,h_idata,mem_size,cudaMemcpyHostToDevice));

float* d_odata;
CUDA_SAFE_CALL(cudaMalloc((void**)&d_odata,mem_size));

dim3 grid(2,2,1);
dim3 threads(2,2,1);

testKernel<<<grid,threads>>>(d_idata,d_odata,4,4);

CUT_CHECK_ERROR("Kernel execution failed");

float* h_odata;
CUDA_SAFE_CALL(cudaMallocHost((void**)&h_odata,mem_size));

CUDA_SAFE_CALL(cudaMemcpy(h_odata,d_odata,mem_size,cudaMemcpyDeviceToHost));

for (unsigned int i=0;i<4;i++)
{
for (unsigned int j=0;j<4;j++)
{
printf("%5.0f",h_odata[i*4+j]);
}
printf("\n");
}
CUDA_SAFE_CALL(cudaFreeHost(h_idata));
CUDA_SAFE_CALL(cudaFreeHost(h_odata));
CUDA_SAFE_CALL(cudaFree(d_idata));
CUDA_SAFE_CALL(cudaFree(d_odata));
}

//example_2_kernel.cu
#ifndef _EXAMPLE_2_KERNEL_H_
#define _EXAMPLE_2_KERNEL_H_
#define SDATA(index) CUT_BANK_CHECKER(sdata,index)
__global__ void testKernel(float* g_idata, float* g_odata, int width, int height)
{
__shared__ float sdata[4];

unsigned int bid_in_grid = __mul24(blockIdx.y,gridDim.x)+blockIdx.x;
unsigned int tid_in_block = __mul24(threadIdx.y,blockDim.x)+threadIdx.x;
unsigned int tid_in_grid_x = __mul24(blockDim.x,blockIdx.x) + threadIdx.x;
unsigned int tid_in_grid_y = __mul24(blockDim.y,blockIdx.y) + threadIdx.y;
unsigned int tid_in_grid = __mul24(tid_in_grid_y,width)+ tid_in_grid_x;

SDATA(tid_in_block)=g_idata[tid_in_grid];
__syncthreads();

g_odata[tid_in_grid]=SDATA(tid_in_block);
}
我实在不知道哪里出错啊,照着书上把代码敲上去的,为什么还是有错呢?
我的环境是cuda3.1
谢谢各位大牛了,帮忙解答解答
...全文
152 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wulonghua 2011-02-07
  • 打赏
  • 举报
回复
唉,这问题不用高手回答了,我自己生搬硬套,粗心大意。。。example_2.cu中省略了一些代码,我没注意,程序都没有给main入口,当然运行不了罗。。。在前面加上函数声明和main函数就可以了:

void runTest(int argc, char ** argv);
int main(int argc, char ** argv)
{
runTest(argc,argv);
CUT_EXIT(argc,argv); //退出CUDA
}

运行了,可是结果跟书上不对应,我自己再慢慢研究

581

社区成员

发帖
与我相关
我的任务
社区描述
CUDA™是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。 它包含了CUDA指令集架构(ISA)以及GPU内部的并行计算引擎。
社区管理员
  • CUDA编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧