building a shared library (Linux/Mac) based on the CUDA SDK common.mk file?

Hi,

I have been trying unsuccessfully to slightly modify the common.mk script to accept another argument, say

SHARED_LIB := 1

to make it build a shared library instead of an executable program.

It seems that simply passing the -shared flag via NVCCFLAGS and CPPFLAGS did not quite work. It just continued outputting an executable binary file.

Is there any trick to achieve this with minimal effort and based on the SDK’s common.mk? We don’t need a full blown libtool based solution here, as this shared library is merely going to be a plugin to be loaded via the dlopen API. It does not need to be installed in /usr/lib and it does not need to install any header files etc.

Christian

Why are you modifying common.mk? It’s ridiculously complicated compared to what you probably need…

(you can pretty much just use nvcc -shared -Xcompiler -fPIC and life is peachy.)

I am instructing my student interns here to work in the SDK folder and start own projects based on the template sample.

I changed the common.mk to allow building shared libraries now.

Sample Makefile to build a shared library follows:

############################################################

#

# Build script for project

#

############################################################

# CUDA source files (compiled with cudacc)

CUFILES		 := template.cu

# CUDA dependency files

CU_DEPS		 := \

		template_kernel.cu \

# C/C++ source files (compiled with gcc / c++)

CCFILES		 := \

		template_gold.cpp \

# we want a shared library built from this code

SHARED_LIB	  := libtemplate.so

############################################################

# Rules and targets

include ../../common/common.mk

The attached .tar.gz file contains a demonstration shared library based on the template sample and a program consuming the library as a “plugin” at runtime using the dlopen API. The runTest function is exported in this library.

Comparably to building a static lib, the shared library file is saved in “lib” (relative to the NVIDIA CUDA SDK folder) - release and debug builds are named differently (libtemplate.so vs. libtemplateD.so).

This is for the SDK version 2.0. There is no guarantee that the changes in common.mk don’t break other things - so keep a backup please

Christian
library_example.tar.gz (6.61 KB)

Why is -fPIC an unknown option for nvcc? I tried:

nvcc -fPIC -c something.cu

and it gave me:

nvcc fatal : Unknown option ‘fPIC’

because it’s a compiler option, not an nvcc option, ergo you have to do -Xcompiler -fPIC

It works. Thanks.

Hello, i’m confused with the creation of shared library.

As i have a project which have some .h .c .cu files.
In the project’s makefile, it created the .o files of all the .c .cu files, just by:

CFLAGS+= -DOPENCV
CFLAGS+= -DCUDNN
ARCH= -gencode arch=compute_53,code=[sm_53,sm_53] 
COMMON+= -DOPENCV
COMMON+= `pkg-config --cflags opencv` 
COMMON+= -DGPU -I/usr/local/cuda/include/
COMMON+= -DCUDNN 
$(OBJDIR)%.o: %.c $(DEPS)
	$(CC) $(COMMON) $(CFLAGS) -c $< -o $@

$(OBJDIR)%.o: %.cu $(DEPS)
	$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@

I had created the static library with all the .c and .cu files by

ar cr libtest.a *.o

and successed.
I want to creating the shared library by the similar way, but have no idea. I think maybe i can’t use “gcc” or “nvcc” alone, what should i do ?