已编辑:(我在编译时没有错误,但使用共享库出现错误或警告)
我有一个从 cpu 移植到 GPU 的函数。我在 port.cu 中定义了我的 cuda 函数,调用 gpu 内核的函数是从 CNVEncoderH264.cpp 调用的。
我想链接这两个文件以创建一个共享库以在另一个程序中使用,这是我的 make 文件:
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 $<
ffmpeg_interface.o: ffmpeg_interface.c ../core/include/nvFileIO.h
$(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $<
NvPthreadABI.o: ../core/threads/NvPthreadABI.cpp
$(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $<
NvThreadingClasses.o: ../core/threads/NvThreadingClasses.cpp
$(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $<
NvThreadingLinux.o: ../core/threads/NvThreadingLinux.cpp
$(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $<
utilities.o: src/utilities.cpp
$(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $<
xcodeutil.o: src/xcodeutil.cpp inc/xcodeutil.h
$(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $<
CNVEncoderH264.o: src/CNVEncoderH264.cpp utilities.o ffmpeg_interface.o inc/CNVEncoder.h
$(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $<
CNVEncoder.o: src/CNVEncoder.cpp inc/CNVEncoder.h
$(GCC) $(CCFLAGS) $(EXTRA_CCFLAGS) $(INCLUDES) -o $@ -c $<
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)
这是我的 make 命令输出:
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
当我在我的程序中使用这个共享库时,我得到这个错误:
symbol lookup error: /usr/local/lib/libnvenc.so: undefined symbol: __cudaRegisterLinkedBinary_39_tmpxft_0000257a_00000000_6_port_cpp1_ii_58a1dcc4
看来,我无法正确链接它们,怎么了?
编辑:之前使用共享库时的命令
enabled libnvenc && require libnvenc x264.h x264_encoder_encode -Wl,-Bdynamic -lnvenc &&
{ check_cpp_condition x264.h "X264_BUILD >= 118" ||
die "ERROR: libnvenc version must be >= 0.118."; }
根据我朋友的建议,我在 -lnvenc 之后添加了 -L/usr/local/cuda/lib64 -lcudart。