将 OpenCL.so 库添加到 makefile,出现错误 "undefined reference to"

Adding OpenCL.so library to a makefile, I get error "undefined reference to"

我正在尝试使用 OpenCL 库将我的源代码从 Windows 编译到 Ubuntu 系统。我可以确认 OpenCL 库已成功安装在 /usr/lib/x86_64-linux-gnu/libOpenCL.so

当我 make release 我得到“ 的未定义引用”尽管事实上我 link 编辑了位于 [=15= 的 OpenCL 文件].

我来自 Visual Studio 世界,所以我所做的就是在原始 makefile 中添加库的路径,使用 DYN_RELEASE_LIBS[=38= 的 -L 参数] 和 STAT_RELEASE_LIBS 变量。

user75@server:~/source/build/linux$ make release
make -C lib/TLibVideoIO              release MM32=0
make[1]: Entering directory `/home/user75/source/build/linux/lib/TLibVideoIO'
make[1]: Leaving directory `/home/user75/source/build/linux/lib/TLibVideoIO'
make -C lib/TLibCommon               release MM32=0
make[1]: Entering directory `/home/user75/source/build/linux/lib/TLibCommon'
make[1]: Leaving directory `/home/user75/source/build/linux/lib/TLibCommon'
make -C lib/TLibDecoder              release MM32=0
make[1]: Entering directory `/home/user75/source/build/linux/lib/TLibDecoder'
make[1]: Leaving directory `/home/user75/source/build/linux/lib/TLibDecoder'
make -C lib/TLibEncoder              release MM32=0
make[1]: Entering directory `/home/user75/source/build/linux/lib/TLibEncoder'
make[1]: Leaving directory `/home/user75/source/build/linux/lib/TLibEncoder'
make -C lib/TAppCommon               release MM32=0
make[1]: Entering directory `/home/user75/source/build/linux/lib/TAppCommon'
make[1]: Leaving directory `/home/user75/source/build/linux/lib/TAppCommon'
make -C app/TAppDecoder              release MM32=0
make[1]: Entering directory `/home/user75/source/build/linux/app/TAppDecoder'
make[1]: Leaving directory `/home/user75/source/build/linux/app/TAppDecoder'
make -C app/TAppEncoder              release MM32=0
make[1]: Entering directory `/home/user75/source/build/linux/app/TAppEncoder'
g++-4.8 -o ../../../../bin/Linux/TAppEncoderStatic -Wall  ./objects/encmain.r.o ./objects/TAppEncCfg.r.o ./objects/TAppEncTop.r.o -L../../../../lib /usr/lib/x86_64-linux-gnu/libOpenCL.so -ldl  -lpthread  -lTLibEncoderStatic -lTLibCommonStatic -lTLibVideoIOStatic -lTAppCommonStatic -L/usr/lib/x86_64-linux-gnu/libOpenCL.so
../../../../lib/libTLibEncoderStatic.a(TEncTop.r.o): In function `TEncTop::xInitOpenCL(int)':
TEncTop.cpp:(.text+0x4264): undefined reference to `TEncOpenCL::findDevice(int)'
TEncTop.cpp:(.text+0x4285): undefined reference to `TEncOpenCL::compileOneKernelSource(char const*, char const*)'
TEncTop.cpp:(.text+0x42e3): undefined reference to `TEncOpenCL::setCTUs_and_SearchRange(unsigned int, unsigned int, int)'
../../../../lib/libTLibEncoderStatic.a(TEncTop.r.o): In function `TEncTop::TEncTop()':
TEncTop.cpp:(.text+0x5597): undefined reference to `TEncOpenCL::TEncOpenCL()'
TEncTop.cpp:(.text+0x58fc): undefined reference to `TEncOpenCL::~TEncOpenCL()'
../../../../lib/libTLibEncoderStatic.a(TEncTop.r.o): In function `TEncTop::~TEncTop()':
TEncTop.cpp:(.text+0x5b74): undefined reference to `TEncOpenCL::~TEncOpenCL()'
TEncTop.cpp:(.text+0x5d47): undefined reference to `TEncOpenCL::~TEncOpenCL()'
../../../../lib/libTLibEncoderStatic.a(TEncSearch.r.o): In function `TEncSearch::xMotionEstimation(TComDataCU*, TComYuv*, int, RefPicList, TComMv*, int, TComMv&, unsigned int&, unsigned int&, TComMv (*) [33][593], unsigned int (*) [33][593], bool, unsigned long, bool)':
TEncSearch.cpp:(.text+0xf28d): undefined reference to `TEncOpenCL::calcMotionVectors(short*, short*, int, int, int, TComMv*, unsigned long, int)'
TEncSearch.cpp:(.text+0xf46f): undefined reference to `TEncOpenCL::FreeMemory(int)'
../../../../lib/libTLibEncoderStatic.a(TEncSearch.r.o): In function `TEncSearch::xMotionEstimation(TComDataCU*, TComYuv*, int, RefPicList, TComMv*, int, TComMv&, unsigned int&, unsigned int&, TComMv (*) [33][593], unsigned int (*) [33][593], bool, unsigned long, bool) [clone .constprop.186]':
TEncSearch.cpp:(.text+0xfd32): undefined reference to `TEncOpenCL::calcMotionVectors(short*, short*, int, int, int, TComMv*, unsigned long, int)'
TEncSearch.cpp:(.text+0xff39): undefined reference to `TEncOpenCL::FreeMemory(int)'
collect2: error: ld returned 1 exit status
make[1]: *** [../../../../bin/Linux/TAppEncoderStatic] Error 1
make[1]: Leaving directory `/home/user75/source/build/linux/app/TAppEncoder'
make: *** [release] Error 2

Makefile源代码如下。我所做的更改是 USER_LIB_DIRSDYN_RELEASE_LIBSSTAT_RELEASE_LIBS变量。

# the SOURCE definiton lets you move your makefile to another position
CONFIG              = CONSOLE

# set directories to your wanted values
SRC_DIR             = ../../../../source/App/TAppEncoder
INC_DIR             = ../../../../source/Lib
LIB_DIR             = ../../../../lib
BIN_DIR             = ../../../../bin

SRC_DIR1        =
SRC_DIR2        =
SRC_DIR3        =
SRC_DIR4        =

USER_INC_DIRS   = -I$(SRC_DIR)
USER_LIB_DIRS   = /usr/lib/x86_64-linux-gnu/libOpenCL.so

ifeq ($(HIGHBITDEPTH), 1)
HBD=HighBitDepth
else
HBD=
endif

# intermediate directory for object files
OBJ_DIR             = ./objects$(HBD)

# set executable name
PRJ_NAME            = TAppEncoder$(HBD)

# defines to set
DEFS                = -DMSYS_LINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -DMSYS_UNIX_LARGEFILE

# set objects
OBJS                =   \
                    $(OBJ_DIR)/encmain.o \
                    $(OBJ_DIR)/TAppEncCfg.o \
                    $(OBJ_DIR)/TAppEncTop.o \

# set libs to link with
LIBS                = -ldl

DEBUG_LIBS          =
RELEASE_LIBS        =

STAT_LIBS           = -lpthread 
DYN_LIBS            =


DYN_DEBUG_LIBS      = -lTLibEncoder$(HBD)d -lTLibCommon$(HBD)d -lTLibVideoIO$(HBD)d -lTAppCommon$(HBD)d
DYN_DEBUG_PREREQS       = $(LIB_DIR)/libTLibEncoder$(HBD)d.a $(LIB_DIR)/libTLibCommon$(HBD)d.a $(LIB_DIR)/libTLibVideoIO$(HBD)d.a $(LIB_DIR)/libTAppCommon$(HBD)d.a
STAT_DEBUG_LIBS     = -lTLibEncoder$(HBD)Staticd -lTLibCommon$(HBD)Staticd -lTLibVideoIO$(HBD)Staticd -lTAppCommon$(HBD)Staticd
STAT_DEBUG_PREREQS      = $(LIB_DIR)/libTLibEncoder$(HBD)Staticd.a $(LIB_DIR)/libTLibCommon$(HBD)Staticd.a $(LIB_DIR)/libTLibVideoIO$(HBD)Staticd.a $(LIB_DIR)/libTAppCommon$(HBD)Staticd.a

DYN_RELEASE_LIBS    = -lTLibEncoder$(HBD) -lTLibCommon$(HBD) -lTLibVideoIO$(HBD) -lTAppCommon$(HBD) -l/usr/lib/x86_64-linux-gnu/libOpenCL.so 
DYN_RELEASE_PREREQS = $(LIB_DIR)/libTLibEncoder$(HBD).a $(LIB_DIR)/libTLibCommon$(HBD).a $(LIB_DIR)/libTLibVideoIO$(HBD).a $(LIB_DIR)/libTAppCommon$(HBD).a
STAT_RELEASE_LIBS   = -lTLibEncoder$(HBD)Static -lTLibCommon$(HBD)Static -lTLibVideoIO$(HBD)Static -lTAppCommon$(HBD)Static -l/usr/lib/x86_64-linux-gnu/libOpenCL.so 
STAT_RELEASE_PREREQS    = $(LIB_DIR)/libTLibEncoder$(HBD)Static.a $(LIB_DIR)/libTLibCommon$(HBD)Static.a $(LIB_DIR)/libTLibVideoIO$(HBD)Static.a $(LIB_DIR)/libTAppCommon$(HBD)Static.a

ifeq ($(EXTENSION_360_VIDEO), 1)
  DYN_DEBUG_LIBS      += -lTAppEncHelper360$(HBD)d -lTLib360$(HBD)d
  DYN_DEBUG_PREREQS   += $(LIB_DIR)/libTAppEncHelper360$(HBD)d.a $(LIB_DIR)/libTLib360$(HBD)d.a
  STAT_DEBUG_LIBS     += -lTAppEncHelper360$(HBD)Staticd -lTLib360$(HBD)Staticd
  STAT_DEBUG_PREREQS  += $(LIB_DIR)/libTAppEncHelper360$(HBD)Staticd.a $(LIB_DIR)/libTLib360$(HBD)Staticd.a
  DYN_RELEASE_LIBS    += -lTAppEncHelper360$(HBD) -lTLib360$(HBD)
  DYN_RELEASE_PREREQS += $(LIB_DIR)/libTAppEncHelper360$(HBD).a $(LIB_DIR)/libTLib360$(HBD).a
  STAT_RELEASE_LIBS   += -lTAppEncHelper360$(HBD)Static -lTLib360$(HBD)Static
  STAT_RELEASE_PREREQS+= $(LIB_DIR)/libTAppEncHelper360$(HBD)Static.a $(LIB_DIR)/libTLib360$(HBD)Static.a
endif

# name of the base makefile
MAKE_FILE_NAME      = ../../common/makefile.base

# include the base makefile
include $(MAKE_FILE_NAME)

所以,问题是 1) 这是 link 错误吗? 2) 如何成功添加*.so库?

原始完整的 makefile 位于 https://hevc.hhi.fraunhofer.de/svn/svn_HEVCSoftware/branches/HM-dev/build/linux/

此致,

共享库应添加为

LDFLAGS += -L/usr/lib/x86_64-linux-gnu/libOpenCL.so
LDLIBS += -lOpenCL
  1. 您的命令行:g++-4.8 -o ../../../../bin/Linux/TAppEncoderStatic -Wall ./objects/encmain.r.o ./objects/TAppEncCfg.r.o ./objects/TAppEncTop.r.o -L../../../../lib /usr/lib/x86_64-linux-gnu/libOpenCL.so -ldl -lpthread -lTLibEncoderStatic -lTLibCommonStatic -lTLibVideoIOStatic -lTAppCommonStatic -L/usr/lib/x86_64-linux-gnu/libOpenCL.so不正确。

    您应该删除 -L/usr/lib/x86_64-linux-gnu/libOpenCL.so,将 /usr/lib/x86_64-linux-gnu/libOpenCL.so 替换为 -lOpenCL,并将其移至末尾。同时删除 -lpthread 并添加 -pthread

    该命令应如下所示:
    g++-4.8 -pthread -o ... -lOpenCL -ldl

  2. 您的问题与 libOpenCL无关 -- 未解析的符号如 TEncOpenCL::findDevice(int) 等看起来像 你自己的个符号。

    这可能意味着 libTLibEncoderStaticlibTLibCommonStatic 等的顺序不正确(阅读 this 以了解正确的顺序),或者您只是缺少这些库中的一些目标文件.

    运行 nm -AC lib/lib*.a | grep TEncOpenCL::findDevice(int) 应该有助于找出哪个库(如果有的话)定义了这个符号。