【发布时间】:2016-12-17 12:01:27
【问题描述】:
我正在开始一个涉及 CUDA 的新项目,我想使用 Glade (v3) 作为 GUI。
我正在尝试在一个基本的 CUDA 模板项目中实现一个 Glade 项目,这是我目前的代码:
#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <gtk/gtk.h>
static void CheckCudaErrorAux (const char *, unsigned, const char *, cudaError_t);
#define CUDA_CHECK_RETURN(value) CheckCudaErrorAux(__FILE__,__LINE__, #value, value)
/**
* CUDA kernel that computes reciprocal values for a given vector
*/
__global__ void reciprocalKernel(float *data, unsigned vectorSize) {
unsigned idx = blockIdx.x*blockDim.x+threadIdx.x;
if (idx < vectorSize)
data[idx] = 1.0/data[idx];
}
/**
* Host function that copies the data and launches the work on GPU
*/
float *gpuReciprocal(float *data, unsigned size)
{
float *rc = new float[size];
float *gpuData;
CUDA_CHECK_RETURN(cudaMalloc((void **)&gpuData, sizeof(float)*size));
CUDA_CHECK_RETURN(cudaMemcpy(gpuData, data, sizeof(float)*size, cudaMemcpyHostToDevice));
static const int BLOCK_SIZE = 256;
const int blockCount = (size+BLOCK_SIZE-1)/BLOCK_SIZE;
reciprocalKernel<<<blockCount, BLOCK_SIZE>>> (gpuData, size);
CUDA_CHECK_RETURN(cudaMemcpy(rc, gpuData, sizeof(float)*size, cudaMemcpyDeviceToHost));
CUDA_CHECK_RETURN(cudaFree(gpuData));
return rc;
}
float *cpuReciprocal(float *data, unsigned size)
{
float *rc = new float[size];
for (unsigned cnt = 0; cnt < size; ++cnt) rc[cnt] = 1.0/data[cnt];
return rc;
}
void initialize(float *data, unsigned size)
{
for (unsigned i = 0; i < size; ++i)
data[i] = .5*(i+1);
}
int main(int argc, char *argv[])
{
static const int WORK_SIZE = 65530;
float *data = new float[WORK_SIZE];
initialize (data, WORK_SIZE);
GtkBuilder *builder;
GtkWidget *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "GladeTest01.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "applicationwindow1"));
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_widget_show(window);
gtk_main();
/*
float *recCpu = cpuReciprocal(data, WORK_SIZE);
float *recGpu = gpuReciprocal(data, WORK_SIZE);
float cpuSum = std::accumulate (recCpu, recCpu+WORK_SIZE, 0.0);
float gpuSum = std::accumulate (recGpu, recGpu+WORK_SIZE, 0.0);
/* Verify the results *//*
std::cout<<"gpuSum = "<<gpuSum<< " cpuSum = " <<cpuSum<<std::endl;
/* Free memory *//*
delete[] data;
delete[] recCpu;
delete[] recGpu;
*/
return 0;
}
// called when window is closed
void on_QuitApp()
{
gtk_main_quit();
}
在编译时,我无法理解为什么会出现这些错误:
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:72: undefined reference to `gtk_init'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:74: undefined reference to `gtk_builder_new'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:75: undefined reference to `gtk_builder_add_from_file'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:77: undefined reference to `gtk_widget_get_type'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:77: undefined reference to `gtk_builder_get_object'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:77: undefined reference to `g_type_check_instance_cast'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:78: undefined reference to `gtk_builder_connect_signals'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:80: undefined reference to `g_object_unref'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:82: undefined reference to `gtk_widget_show'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:83: undefined reference to `gtk_main'
./src/GladeTest.o: In function `on_QuitApp()':
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:105: undefined reference to `gtk_main_quit'
collect2: error: ld returned 1 exit status
make: *** [GladeTest] Error 1
因为一切似乎都在 gtk.h 中定义,而所有其他内容都包含在其中... 我在这里错过了什么?
有人能给我一些指示或告诉我我做错了什么吗?
编辑: 这是使用的编译命令:
Invoking: NVCC Compiler
/usr/local/cuda-7.5/bin/nvcc -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/glib-2.0 -G -g -O3 -gencode arch=compute_52,code=sm_52 -m64 -odir "src" -M -o "src/GladeTest.d" "../src/GladeTest.cu"
/usr/local/cuda-7.5/bin/nvcc -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/glib-2.0 -G -g -O3 --compile --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -m64 -x cu -o "src/GladeTest.o" "../src/GladeTest.cu"
Finished building: ../src/GladeTest.cu
Building target: GladeTest
Invoking: NVCC Linker
/usr/local/cuda-7.5/bin/nvcc --cudart static --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -m64 -link -o "GladeTest" ./src/GladeTest.o
如果我正确理解了答案,所有 -I 选项都应该在 nvcc 链接器调用中,而不是在 nvcc 编译器命令中?
编辑 2: 这是我尝试过的众多变体之一。每次都得到相同的结果:
Invoking: NVCC Compiler
/usr/local/cuda-7.5/bin/nvcc -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/atk-1.0 -G -g -O3 -gencode arch=compute_52,code=sm_52 -m64 -odir "src" -M -o "src/GladeTest.d" "../src/GladeTest.cu"
/usr/local/cuda-7.5/bin/nvcc -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/atk-1.0 -G -g -O3 --compile --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -m64 -x cu -o "src/GladeTest.o" "../src/GladeTest.cu"
Finished building: ../src/GladeTest.cu
Building target: GladeTest
Invoking: NVCC Linker
/usr/local/cuda-7.5/bin/nvcc --cudart static -L/usr/include/gtk-3.0 -L/usr/include/glib-2.0 -L/usr/lib/x86_64-linux-gnu/glib-2.0/include -L/usr/include/pango-1.0 -L/usr/include/cairo -L/usr/include/gdk-pixbuf-2.0 -L/usr/include/atk-1.0 -lGL -lGLU -lglut --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -m64 -link -o "GladeTest" ./src/GladeTest.o
【问题讨论】:
-
请显示您的编译命令。
-
那是链接错误,不是编译错误。您必须链接相关的 GTK 库