在CUDA Toolkit 5中创建.lib文件

时间:2012-09-19 17:25:45

标签: visual-studio-2010 cuda static-libraries

我正在使用VS2010进行CUDA Toolkit 5.0 RC的第一步步骤。

单独的编译让我很困惑。

我尝试将项目设置为静态库(.lib),但是当我尝试构建它时,它不会创建device-link.obj,我不明白为什么。

例如,有2个文件:

使用函数f

的调用函数
#include "thrust\host_vector.h"
#include "thrust\device_vector.h"
using namespace thrust::placeholders;

extern __device__ double f(double x);

struct f_func 
{
__device__ double operator()(const double& x) const
{
    return f(x);
}
};

void test(const int len, double * data, double * res)
{
thrust::device_vector<double> d_data(data, data + len);
thrust::transform(d_data.begin(), d_data.end(), d_data.begin(), f_func());
thrust::copy(d_data.begin(),d_data.end(), res);
}

定义f

的库文件
__device__ double f(double x)
{
return x+2.0;
}

如果我将选项生成可重定位设备代码设置为No,则由于未解析的extern函数f,第一个文件将无法编译。

如果我将它设置为-rdc,它将编译,但不会产生device-link.obj文件,因此链接器会失败。

如果我将f的定义放入第一个文件并删除第二个文件,那么它就会成功构建,但现在它不再是单独的编译了。

如何使用单独的源文件构建这样的静态库?


[在此更新]

我调用了第一个调用者文件“caller.cu”和第二个“libfn.cu”。 VS2010输出的编译器行(我不完全理解)是(对于调用者):

nvcc.exe 
-ccbin "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin"  
-I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include" 
-I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include"  
-G   
--keep-dir "Debug" 
-maxrregcount=0  
--machine 32 
--compile  
-g   
-D_MBCS 
-Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " 
-o "Debug\caller.cu.obj" "G:\Test_Linking\caller.cu" 
-clean

和libfn一样,然后:

nvcc.exe 
-gencode=arch=compute_20,code=\"sm_20,compute_20\" 
--use-local-env 
--cl-version 2010 
-ccbin "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin" 
-rdc=true 
-I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include" 
-I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include"  
-G   
--keep-dir "Debug" 
-maxrregcount=0  
--machine 32 
--compile  
-g   
-D_MBCS 
-Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " 
-o "Debug\caller.cu.obj" "G:\Test_Linking\caller.cu"

再次为libfn。

1 个答案:

答案 0 :(得分:1)

如果您不使用Visual Studio 2010 IDE,则可以按照

中的@njuffa程序进行操作

How to create a static lib for device functions using cuda 5.0?