[英]Compiling CUDA code while linking a static library
I have C code main_code.c
and helper_code.c
.我有 C 代码
main_code.c
和helper_code.c
。 The former depends on some CUDA code cuda_code.cu
and the latter on an external library mylib
.前者依赖于一些 CUDA 代码
cuda_code.cu
,后者依赖于外部库mylib
。 For my external library mylib
to work, I need to link it to my code with the -static
flag:为了让我的外部库
mylib
工作,我需要使用-static
标志将它链接到我的代码:
g++ main_code.c helper_code.c -o main_code -static -L/usr/local/mylib/lib -lmylib -lmylib2
But main_code.c
also depends on CUDA code- cuda_code.cu
.但是
main_code.c
也依赖于 CUDA 代码 - cuda_code.cu
。 I can link it with:我可以将它链接到:
nvcc cuda_code.cu -c
g++ main_code.c -o main_code cuda_code.o -L/usr/local/cuda-10.0/lib64 -lcudart -lpthread
I want to compile my code together with the CUDA code and the external library mylib
.我想将我的代码与 CUDA 代码和外部库
mylib
一起编译。 However, linking mylib
works only with the -static
flag.但是,链接
mylib
仅适用于-static
标志。 A naive attempt would be the following but it does not work:天真的尝试如下,但它不起作用:
nvcc cuda_code.cu -c
g++ main_code.c helper_code.c -o main_code cuda_code.o -static -L/usr/local/mylib/lib -lmylib -lmylib2 -L/usr/local/cuda-10.0/lib64 -lcudart -lpthread
This gives the error:这给出了错误:
/usr/bin/ld: cannot find -lcudart
which I'm assuming is because you cannot use the static flag while linking with CUDA (because it goes away when I remove the -static
flag (in addition to also removing the mylib
library linking)).我假设这是因为在与 CUDA 链接时不能使用静态标志(因为当我删除
-static
标志时它会消失(除了还删除mylib
库链接))。
I then tried compiling helper_code.c
separately and then linking it to main_code.c
since it's just helper_code.c
that needs mylib
:然后我尝试单独编译
helper_code.c
然后将它链接到main_code.c
因为它只是helper_code.c
需要mylib
:
helper.o:
g++ helper_code.c -c -static -L/usr/local/mylib/lib -lmylib -lmylib2
cuda-code.o:
nvcc cuda_code.cu -c
main-code: helper.o cuda-code.o
g++ main_code.c -o main_code helper_code.o cuda_code.o -L/usr/local/cuda-10.0/lib64 -lcudart -lpthread
But this also does not work.但这也行不通。 I get an
undefined reference
error that's referring to a function defined in mylib
, meaning the linking to mylib
isn't working.我收到一个
undefined reference
错误,它指的是mylib
定义的函数,这意味着到mylib
的链接不起作用。 I can resolve that error by including the mylib
library and using the -static
flag but that then breaks the CUDA linking.我可以通过包含
mylib
库并使用-static
标志来解决该错误,但这会破坏 CUDA 链接。
I can separately get the CUDA linking (to cuda_code.cu
) to work or mylib
linking to work but not both at the same time.我可以分别让 CUDA 链接(到
cuda_code.cu
)工作或mylib
链接工作,但不能同时进行。
So is there a workaround to link mylib
(which needs -static
) while at the same time also linking my CUDA code (which does not allow -static
)?那么是否有一种解决方法可以链接
mylib
(需要-static
),同时还要链接我的 CUDA 代码(不允许-static
)?
按照 talonmies 评论中链接的答案,以下内容成功了:
g++ main_code.c -o main_code helper_code.o cuda_code.o -L/usr/local/mylib/lib -L/usr/local/cuda-10.0/lib64 -Wl,-Bstatic -lmylib -lmylib2 -Wl,-Bdynamic -lcudart
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.