CUDA C ++链接错误未定义参考threadIdx.x和blockDim.x

问题描述 投票:0回答:1

我正在尝试使用CUDA for OpenGL应用程序编译我的代码。这是我的代码。

#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <device_launch_parameters.h>

float t = 0.0f;   //timer
float3* device;   //pointer to memory on the device (GPU VRAM)
GLuint buffer;   //buffer

__global__ void demo(int * d_a)
{
    int a=blockDim.x * blockIdx.x + threadIdx.x;
}

我有一个错误

/usr/bin/ld: CMakeFiles/MatchSlide.dir/main.cpp.o: in function `demo(void*)':
main.cpp:33: undefined reference to `blockDim'
/usr/bin/ld: main.cpp:33: undefined reference to `blockIdx'
/usr/bin/ld: main.cpp:33: undefined reference to `threadIdx'

也在CMakeFile中,我也定义了

# Look for Truetype && libpng
find_package(Freetype REQUIRED)
include_directories(${FREETYPE_INCLUDE_DIRS})

pkg_check_modules(PNG REQUIRED libpng)
include_directories(${PNG_INCLUDE_DIRS})
find_package(CUDA  REQUIRED)
include_directories("${CUDA_INCLUDE_DIRS}")

set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -lcurand -lglfw -lGLEW -lGLU -lGL -lglut -lfreetype -lpng -lpthread -lX11  -L/usr/local/cuda/lib64 -lcudart  -lcuda")
/**
 * User:Demo
 * Date: 30/4/20.
 * Sample Project
 */

// Headers

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "Event.h"
#include "ResourceManager.h"
#include <cstdio>
#include <cstring>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <device_launch_parameters.h>

float t = 0.0f;   //timer
float3* device;   //pointer to memory on the device (GPU VRAM)
GLuint buffer;   //buffer


__global__ void rendering(float3 *output,float k)
{
    int a=blockDim.x * blockIdx.x + threadIdx.x;
}

您可以在我的代码中看到,我试图定义一个简单的线程函数来运行我的代码。但是它会生成错误,可以帮助我确定在应用程序中定义Cuda时在哪里出错。

c++ cuda
1个回答
0
投票

请阅读nvcc支持的文件扩展名上的here。如果您的源文件同时包含主机代码和设备功能,则不再仅仅是C ++源文件。我能想到的三种解决方案:

1-将main.cpp更改为main.cu。但是,如果您确实不想将main.cpp更改为main.cu(不确定原因),请考虑以下两个选项:

2-将您的内核demo包含在扩展名为cu的辅助文件中,

3-用选项.cpp照原样编译x cu文件,例如:

nvcc main.cpp -x cu

通过它强制编译器将main.cpp文件视为CUDA源文件,即使扩展名是cpp

© www.soinside.com 2019 - 2024. All rights reserved.