I have C code main_code.c
and helper_code.c
. The former depends on some CUDA code cuda_code.cu
and the latter on an external library mylib
. For my external library mylib
to work, I need to link it to my code with the -static
flag:
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
. 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
. However, linking mylib
works only with the -static
flag. 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)).
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.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. I can resolve that error by including the mylib
library and using the -static
flag but that then breaks the CUDA linking.
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.
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
)?
Following the answer linked in talonmies's comment, the following did the trick:
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