How To Link A Shared Library (.so) To A CUDA Source File (.cu) Using Nvcc?
Currently, I'm compiling my CUDA source file using:
nvcc -O3 --shared -Xcompiler -fPIC -o CuFile.so CuFile.cu
I'd like to call functions from a shared library called GlobalFunctions.so
.
How can I link the library to the CUDA source file?
So far, I've tried
nvcc -O3 --shared -Xcompiler -fPIC -lGlobalFunctions -o CuFile.so CuFile.cu
and a few variants, but nothing worked.
Answer
It seems evident to me that you are making some mistakes in library naming/usage/referencing, but since you don't show any actual output, I'll just make a suggestion. The changes below should make evident some of the errors (e.g. not preceding library name with lib
, not specifying -L.
or similar, etc.) I suspect none of this is unique or specific to CUDA/nvcc
, as the behavior and command syntax with e.g. g++
should be more or less the same.
The following is one possible approach:
$ cat foo.cu
#include <stdio.h>
#include "foo.h"
void foo(){
printf("Hello from foo\n");
}
$ cat foo.h
void foo();
$ cat CuFile.cu
#include "foo.h"
#include "CuFile.h"
void bar(){
foo();
}
$ cat CuFile.h
void bar();
$ cat test.cu
#include "CuFile.h"
int main(){
bar();
}
$ nvcc -O3 --shared -Xcompiler -fPIC -o libGlobalFunctions.so foo.cu
$ nvcc -O3 --shared -Xcompiler -fPIC CuFile.cu -L. -lGlobalFunctions -o libCuFile.so
$ nvcc -O3 test.cu -o test -L. -lCuFile
$ ./test
Hello from foo
$
If your intermediate library is actually called GlobalFunctions.so
and not libGlobalFunctions.so
(and you don't wish to rename it) then one possible approach is just to pass it directly:
$ nvcc -O3 --shared -Xcompiler -fPIC CuFile.cu GlobalFunctions.so -o libCuFile.so
The -l
switch for g++
and nvcc
when specified like this: -lxyz
will look for a library by the name of libxyz.so
or libxyz.a
. So if your library name doesn't begin with lib
, then I don't know how to use the -l
switch to reference it (perhaps there is a way). Also, when you have a library that is in the current directory, and you attempt to refer to that library with -l
you must also use -L.
to tell the linker to look there for that library. The linker doesn't automatically search the current directory for referenced libraries.
Again, I think all of this applies equally to g++
, so if you can figure out how to do it with g++
you can probably figure out how to do it with nvcc
.
Related Questions
- → Comparing two large files are taking over four hours
- → Setting JSON node name to variable value
- → Compiling GLUT using Emscripten
- → Evaluate check box from a scanned image in node.js
- → Find an easy web server framework for mobile game
- → my https C++ code doesn't work on some sites (binance)
- → Error while opening pivx wallet on ubuntu
- → Why sending a POST by AJAX is interpreted by the HTTP Server as OPTIONS and sending by CURL is effectively a PUT?
- → Python reading in one line multiple types for a calculator
- → How do I properly pass an argument to a function
- → Accessing Websql database with Qt
- → Using Mysql C API for c++ codes
- → How do I set constants at run-time in a c++ header file, imported through Cython?