Build a CUDA program with linking a static lib on windows.

I have a and a static lib on windows. How can nvcc link the lib to a CUDA program?

dirs:, libfoo.lib, foo.h.(foo.h contains void foo() and libfoo contains foo(){}, refers to the function foo() )
then try to compile “nvcc -I. -llibfoo”. But it reports “error LNK2019: unresolved external symbol”.

fllowing commandline succesfully makes a.exe:

nvcc -c
lib /out:libfoo.lib foo.obj
nvcc -llibfoo

Hi episteme,

Thanks for your replay.
First I don’t have there. Only, libfoo.lib and foo.h.
Second, I tried you methods abover:

  1. nvcc -c (will generate main.obj)
  2. lib /out:libfoo.lib main.obj
  3. nvcc -llibfoo

It still doesn’t work and reports “LNK2019: unresolved external symbol”.

if you already have a libfoo.lib you don’t need to do this:

lib /out:libfoo.lib main.obj

and besides that, maybe you actually have an unresolved symbol. Just because you’re getting that error does not necessarily mean your linking process is wrong

Hi txbob,
Thanks for your replay.
I’d like to make sure with you that if modify to to main.c(just file name. Everything is same) and build them with command “cl main.c libfoo.lib” and it works fine!
So I can be sure that libfoo.lib is a good lib.

So, I’ve switched to CMake and it turns out that CMake supports CUDA as well. I’m linking with the static library, cudadevrt. First, make sure the lib is in a directory where you computer knows where to look. For me on Linux I had to manually create a symbolic link to the static library in /usr/lib so I wound up with /usr/lib/libcudadevrt.a

I really, really recommend CMake because they make linking a relatively painless process once you actually have the libraries in the proper locations. Here’s a link to my CMakeLists.txt file.

This is the relevant part:

  regulus # main executable name
  cudadevrt # the static library I care about

Thanks for all guys kindly reply, i have mistakenly to link a 32bit lib, and nvcc defalut to link the 64bit lib, and i have to recompile a 64bit libfoo.lib, it works now.