How to link host code with static CUDA library after separation compilation?

Well, I have a really troubling CUDA 5.0 question about how to put things together correctly. I would really appreciate any help!

Using the shared compilation functions of CUDA 5.0, I created a static library (* .a). This is well connected with other * .cu files when running through nvcc, I have done this many times.

Now I would like to take the * .cpp file and link it to the host code in this static library using g ++ or something else but not nvcc. If I try to do this, I get compiler errors like

undefined link to __cudaRegisterLinkedBinary

I use both -lcuda and -lcudart , and as far as I know, the libraries are in the correct order (which means -lmylib -lcuda -lcudart ). I do not think this is a problem. Maybe I'm wrong, but I feel that I am missing a step and that I need to do something else in my static library (device binding?) Before I can use it with g ++.

Did I miss something important? Is it possible?

Bonus question: I want the end result to be a dynamic library. How can I achieve this?

+7
source share
2 answers

When you contact nvcc, it makes an implicit link to the device along with a link to the host. If you use the host compiler for the link (e.g. with g ++), you need to add an explicit step to link to the device with the -dlink option, for example.

 nvcc –arch=sm_35 –dc a.cu b.cu nvcc –arch=sm_35 –dlink ao bo –o dlink.o g++ ao bo dlink.o x.cpp –lcudart 

Here is an example of this in the chapter Using a separate compilation of an nvcc document .

Currently, we only support static libraries for roaming device code. Wed You are interested in knowing how you want to use such code in a dynamic library. Please feel free to reply in the comments.

Edit:

To answer the question in the comment below: "Is there a way to use nvcc to turn mylib.a into something that can be put into g ++?"

Just use the library as an object, for example:

 nvcc –arch=sm_35 –dlink mylib.a –o dlink.o g++ mylib.a dlink.o x.cpp –lcudart 
+9
source

You can use libraries wherever you use objects. So just do nvcc -arch = sm_35 -dlink mylib.a -o dlink.o g ++ mylib.a dlink.o x.cpp -lcudart

0
source

Source: https://habr.com/ru/post/943922/


All Articles