将 Glade 与 CUDA 编译器 (Nvidia Eclipse Nsight) 结合使用 -> 哪个 NVCC 链接器设置?

Using Glade with CUDA compiler (Nvidia Eclipse Nsight) -> which NVCC linker settings?

我正在开始一个涉及 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 库。

我能够根据您在 Ubuntu 系统上针对 GTK2 发布的代码(稍作修改)生成可执行文件,如下所示:

$ nvcc -arch=sm_52 -Xcompiler="`pkg-config --cflags gtk+-2.0`" cuda_glade.cu `pkg-config --libs gtk+-2.0`

$ ldd a.out
    linux-vdso.so.1 =>  (0x00007ffdf351c000)
    libgtk-x11-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0 (0x00007f85eece0000)
    libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007f85eea8e000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f85ee886000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f85ee668000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f85ee464000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f85ee15f000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f85edd9a000)
    libgdk-x11-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0 (0x00007f85edae7000)
    libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007f85ed8e2000)
    libpangocairo-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangocairo-1.0.so.0 (0x00007f85ed6d5000)
    libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f85ed3a0000)
    libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f85ed199000)
    libatk-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libatk-1.0.so.0 (0x00007f85ecf77000)
    libcairo.so.2 => /usr/lib/x86_64-linux-gnu/libcairo.so.2 (0x00007f85ecc6c000)
    libgdk_pixbuf-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0 (0x00007f85eca4a000)
    libgio-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 (0x00007f85ec6d7000)
    libpangoft2-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0 (0x00007f85ec4c2000)
    libpango-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpango-1.0.so.0 (0x00007f85ec274000)
    libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f85ec038000)
    libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f85ebd30000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f85eba29000)
    libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007f85eb821000)
    /lib64/ld-linux-x86-64.so.2 (0x000055d54fa62000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f85eb60a000)
    libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f85eb400000)
    libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f85eb1fd000)
    libXi.so.6 => /usr/lib/x86_64-linux-gnu/libXi.so.6 (0x00007f85eafec000)
    libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f85eade2000)
    libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f85eabd8000)
    libXcomposite.so.1 => /usr/lib/x86_64-linux-gnu/libXcomposite.so.1 (0x00007f85ea9d4000)
    libXdamage.so.1 => /usr/lib/x86_64-linux-gnu/libXdamage.so.1 (0x00007f85ea7d1000)
    libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f85ea5bf000)
    libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f85ea31b000)
    libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f85ea0fc000)
    libpixman-1.so.0 => /usr/lib/x86_64-linux-gnu/libpixman-1.so.0 (0x00007f85e9e53000)
    libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007f85e9c2d000)
    libxcb-shm.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0 (0x00007f85e9a2a000)
    libxcb-render.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-render.so.0 (0x00007f85e9820000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f85e9607000)
    libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f85e93e3000)
    libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f85e91c8000)
    libharfbuzz.so.0 => /usr/lib/x86_64-linux-gnu/libharfbuzz.so.0 (0x00007f85e8f73000)
    libthai.so.0 => /usr/lib/x86_64-linux-gnu/libthai.so.0 (0x00007f85e8d69000)
    libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f85e8b3f000)
    libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f85e8901000)
    libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f85e86fc000)
    libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f85e84f6000)
    libgraphite2.so.3 => /usr/lib/x86_64-linux-gnu/libgraphite2.so.3 (0x00007f85e82cf000)
    libdatrie.so.1 => /usr/lib/x86_64-linux-gnu/libdatrie.so.1 (0x00007f85e80c8000)

这里我使用 pkg-config 获取必要的编译器标志和链接器依赖项,并将它们直接传递给 nvcc。如果您无法访问 pkg-config,您可以手动构建它们。重要的一点是将库依赖项 放在您自己的代码之后 。 GNU 链接器从左到右读取依赖项,如果您在 before 依赖它的代码中指定一个库,链接器将丢弃该库并导致链接错误。


编辑:在 Nsight Eclipse 版本中执行此操作

使用 $(shell pkg-config ...) 在编译器和链接器阶段的项目属性中添加 pkg-config 命令:

使用您的代码构建项目会给出此构建日志:

make all 
Building file: ../src/glade_cuda.cu
Invoking: NVCC Compiler
/opt/cuda-7.5/bin/nvcc -O3 -Xcompiler -fPIC -Xcompiler -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/libpng12 -I/usr/include/harfbuzz   -gencode arch=compute_52,code=sm_52  -odir "src" -M -o "src/glade_cuda.d" "../src/glade_cuda.cu"
/opt/cuda-7.5/bin/nvcc -O3 -Xcompiler -fPIC -Xcompiler -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/libpng12 -I/usr/include/harfbuzz   --compile --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52  -x cu -o  "src/glade_cuda.o" "../src/glade_cuda.cu"
Finished building: ../src/glade_cuda.cu

Building target: libGTK test
Invoking: NVCC Linker
/opt/cuda-7.5/bin/nvcc --cudart static -shared -Xlinker -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lgobject-2.0 -lglib-2.0 -lfreetype   --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -link -o  "libGTK test"  ./src/glade_cuda.o   
Finished building target: libGTK test


12:57:43 Build Finished (took 3s.963ms)