编辑:(我在编译时没有错误,但使用共享库会出现错误或警告)
我有一个从cpu移植到GPU的功能。我已经在port.cu中定义了我的cuda函数,并且从CNVEncoderH264.cpp调用了调用gpu内核的函数。
我想将这两个文件链接起来以便创建一个共享库以便在另一个程序中使用,这是我的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。
答案 0 :(得分:1)
您正在尝试编译c和c ++文件。
默认情况下,c ++是l ++的c ++函数签名,因此无法找到c函数。
您需要将原型声明为c原型
从c ++文件中尝试这样的事情:
extern "C" {
#include "header.h" //header contain c prototypes
}
你也可以尝试只为你正在调用的函数设置原型:
在file.cpp :(而不是包含)
extern "C" {
//lets asume this is the prototype
void convert(int x, char y);
}
修改强>
您缺少链接的共享标记:
g++ -shared -Wl,-soname,libtest.so -o libtest.so.0 *.o
之后:
set LD_LIBRARY_PATH = /path/to/so
(路径中没有文件)
如果您拥有root权限,则为OR:
/sbin/ldconfig /path/to/so
(再次,没有路径中的文件)