简体   繁体   English

在链接静态库的同时编译 CUDA 代码

[英]Compiling CUDA code while linking a static library

I have C code main_code.c and helper_code.c .我有 C 代码main_code.chelper_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.

相关问题 在Linux上将CUDA代码编译为静态库(.a) - Compiling CUDA-code to a static library (.a) on linux 使用静态库链接,编译和运行ac程序 - linking, compiling and running a c program with a static library 编译和链接纯C和CUDA代码[警告:隐式函数声明] - Compiling and Linking pure C and CUDA code [warning: implicit declaration of function] 将CUDA静态或共享库与gcc链接的未定义引用错误 - undefined reference error for linking CUDA static or shared library with gcc 在keil中编译代码时遇到链接错误 - Facing linking error while compiling the code in keil 链接到 static 库时未定义的函数引用 - Undefined reference to functions while linking to static library 链接静态C库时未定义的引用 - Undefined reference while linking static C library 如何构建我以后可以在使用 emcc 编译代码时使用的静态库? - How to build static library that I can use later on while compiling code with emcc? 链接到共享库时,不会导出 static 库中的符号 - Symbols from a static library are not exported while linking to a shared library 编译调用extern库的cython代码导致的链接错误 - linking error from compiling a cython code calling extern library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM