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

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

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

fllowing commandline succesfully makes a.exe:

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

Hi episteme,

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

  1. nvcc -c main.cu (will generate main.obj)
  2. lib /out:libfoo.lib main.obj
  3. nvcc main.cu -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 main.cu 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:

target_link_libraries(
  regulus # main executable name
  ${CUDA_LIBRARIES}
  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.