【问题标题】:cuda undefined reference to __constant__ arraycuda 未定义对 __constant__ 数组的引用
【发布时间】:2013-02-02 14:11:41
【问题描述】:

我在使用 nvcc 时遇到了以下问题。 我使用单独的编译(最后让它与cmake 一起运行)并且在__device__ __constant__ (extern) type1<type2> var[length] 数组的声明中遇到了问题。

这是我的标题:

#include <type.h>
#include <type2.h>

#ifndef GUARD_H_
#define GUARD_H_

namespace NameSpace1{
  namespace NameSpace2{

    namespace Constants{
      __device__ __constant__ extern Type1<Type2> array[10];
      __device__ Type1<Type2> accessConstantX(size_t index);
    }
  }
}
#endif

还有她我的 .cu 文件:

#include <header.h>
#include <assert.h>

namespace NameSpace1{
  namespace NameSpace2{

    namespace Constants{
      __device__ Type1<Type2> accessConstantX(size_t index){
    assert(index <= 9);
    return array[index];
      }
    }
  }
}

我从中间单独编译步骤中得到以下错误:

nvlink error   : Undefined reference to '_ZN12NameSpace115NameSpace29Constants17arrayE'

这是访问 .cu 文件的结果。 感谢您的建议。

【问题讨论】:

  • 由于命名空间差异 'NameSpace2' ?
  • 无法声明恒定内存externAFAIK。
  • @rps 更正了,这是我帖子中的错字(已更正),但在我的代码中是正确的。
  • @talonmies 感谢我尝试不将其声明为 extern 的提示,但随后我遇到了多个定义的问题。我不明白为什么,因为我只在头文件中定义它,并想从一个包含基于主机的初始化的 .cu 文件和另一个上面的文件访问它,以访问我的内核中的值。知道如何实现我的需要吗?感谢您迄今为止的帮助。
  • 您必须在某处定义它,只需要一次。当您将它声明为extern 时,它并没有在任何地方定义。没有它是 extern 你到处都在定义它。

标签: linker cuda undefined-reference


【解决方案1】:

我发现了我的错误,正在阅读this post。 原来我不明白如何在头文件中正确声明全局变量。我的问题与 nvcc 无关。感谢@talonmies 的帮助,它为我指明了寻找答案的方向。

这里是我的问题的解决方案:

这是我的标题:

#include <type.h>
#include <type2.h>

#ifndef GUARD_H_
#define GUARD_H_

namespace NameSpace1{
  namespace NameSpace2{

    namespace Constants{
      extern __device__ __constant__ extern Type1<Type2> array[10];
      __device__ Type1<Type2> accessConstantX(size_t index);
    }
  }
}
#endif

还有她我的 .cu 文件:

#include <header.h>
#include <assert.h>

namespace NameSpace1{
  namespace NameSpace2{

    namespace Constants{
      __device__ __constant__ Type1<Type2> array[10];
      __device__ Type1<Type2> accessConstantX(size_t index){
        assert(index <= 9);
        return array[index];
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2020-05-05
    • 2014-11-16
    • 1970-01-01
    • 2014-08-14
    • 2013-06-21
    • 2014-07-19
    相关资源
    最近更新 更多