title: Makefile for creating C++ shared library with cuda functions
body: <p>EDITED:(I dont have error in compile time but using share library come to error or warning)</p>
  
  <p>I have a function that I have ported from cpu to GPU. I have defined my cuda functions in port.cu and  the functions that call the gpu kernel is invoked from CNVEncoderH264.cpp. </p>
  
  <p>I want to link these two file to gather in order to create an shared library to use in another program , this is my make file:</p>
  
  <pre><code>    port.o: port.cu
          nvcc -m64 -arch=sm_20 -dc -Xcompiler '-fPIC' port.cu -c -o port.o
          nvcc port.cu -c -o cudafile.o
  main.o: main.cpp ../core/include/nvFileIO.h
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  ffmpeg_interface.o: ffmpeg_interface.c ../core/include/nvFileIO.h
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  
  NvPthreadABI.o: ../core/threads/NvPthreadABI.cpp
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  NvThreadingClasses.o: ../core/threads/NvThreadingClasses.cpp
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  NvThreadingLinux.o: ../core/threads/NvThreadingLinux.cpp
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  utilities.o: src/utilities.cpp
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  xcodeutil.o: src/xcodeutil.cpp inc/xcodeutil.h
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  CNVEncoderH264.o: src/CNVEncoderH264.cpp utilities.o ffmpeg_interface.o inc/CNVEncoder.h
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  
  CNVEncoder.o: src/CNVEncoder.cpp inc/CNVEncoder.h
          $(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $&lt;
  
  libnvenc.so:  port.o NvPthreadABI.o NvThreadingClasses.o NvThreadingLinux.o xcodeutil.o CNVEncoder.o CNVEncoderH264.o utilities.o ffmpeg_interface.o
          gcc  -shared  -Wl,-soname,$@ -o $@  port.o  NvPthreadABI.o NvThreadingClasses.o NvThreadingLinux.o xcodeutil.o CNVEncoder.o CNVEncoderH264.o utilities.o ffmpeg_interface.o -lstdc++  -lnvidia-encode -lcuda -lcudart $(LDFLAGS) $(EXTRA_LDFLAGS)
  </code></pre>
  
  <p>this is my output of make command :</p>
  
  <pre><code>    nvcc -m64 -arch=sm_20 -dc -Xcompiler '-fPIC' port.cu -c -o port.o
  nvcc port.cu -c -o cudafile.o
  g++ -m64 -O0 -ggdb3 -fPIC -fopenmp  -I/usr/local/cuda/include -I./include -I../inc -I../core -I../core/include -I./inc -I . -o NvPthreadABI.o -c ../core/threads/NvPthreadABI.cpp
  g++ -m64 -O0 -ggdb3 -fPIC -fopenmp  -I/usr/local/cuda/include -I./include -I../inc -I../core -I../core/include -I./inc -I . -o NvThreadingClasses.o -c ../core/threads/NvThreadingClasses.cpp
  g++ -m64 -O0 -ggdb3 -fPIC -fopenmp  -I/usr/local/cuda/include -I./include -I../inc -I../core -I../core/include -I./inc -I . -o NvThreadingLinux.o -c ../core/threads/NvThreadingLinux.cpp
  g++ -m64 -O0 -ggdb3 -fPIC -fopenmp  -I/usr/local/cuda/include -I./include -I../inc -I../core -I../core/include -I./inc -I . -o xcodeutil.o -c src/xcodeutil.cpp
  g++ -m64 -O0 -ggdb3 -fPIC -fopenmp  -I/usr/local/cuda/include -I./include -I../inc -I../core -I../core/include -I./inc -I . -o CNVEncoder.o -c src/CNVEncoder.cpp
  g++ -m64 -O0 -ggdb3 -fPIC -fopenmp  -I/usr/local/cuda/include -I./include -I../inc -I../core -I../core/include -I./inc -I . -o utilities.o -c src/utilities.cpp
  g++ -m64 -O0 -ggdb3 -fPIC -fopenmp  -I/usr/local/cuda/include -I./include -I../inc -I../core -I../core/include -I./inc -I . -o ffmpeg_interface.o -c ffmpeg_interface.c
  g++ -m64 -O0 -ggdb3 -fPIC -fopenmp  -I/usr/local/cuda/include -I./include -I../inc -I../core -I../core/include -I./inc -I . -o CNVEncoderH264.o -c src/CNVEncoderH264.cpp
  gcc  -shared  -Wl,-soname,libnvenc.so -o libnvenc.so  port.o  NvPthreadABI.o NvThreadingClasses.o NvThreadingLinux.o xcodeutil.o CNVEncoder.o CNVEncoderH264.o utilities.o ffmpeg_interface.o -lstdc++  -lnvidia-encode -lcuda -lcudart -L/usr/local/cuda/lib64 -lcuda -lcudart -L/usr/lib64 -lnvidia-encode -ldl  
  </code></pre>
  
  <p>when I use this shared library in my program I get this error : </p>
  
  <pre><code> symbol lookup error: /usr/local/lib/libnvenc.so: undefined symbol: __cudaRegisterLinkedBinary_39_tmpxft_0000257a_00000000_6_port_cpp1_ii_58a1dcc4
  </code></pre>
  
  <p>it seems , I couldn't link them properly , what's wrong ?</p>
  
  <p>Edit :
  the command when use shared library before </p>
  
  <pre><code>enabled libnvenc    &amp;&amp; require  libnvenc x264.h x264_encoder_encode -Wl,-Bdynamic -lnvenc  &amp;&amp;
                        { check_cpp_condition x264.h "X264_BUILD &gt;= 118" ||
                          die "ERROR: libnvenc version must be &gt;= 0.118."; }
  </code></pre>
  
  <p>after my friends suggestion I add -L/usr/local/cuda/lib64 -lcudart after the -lnvenc.</p>