Makefile error with gstreamer shared library

Question/Issue: Compilation error in edge impulse Makefile on jetson nano after merging with a shared library from gstreamer. Given below is snippet from Makefile. The CCObjects linking in the last line gives compilation error.

all: $(LIB)

#all: runner

#.PHONY: runner clean

#$(LIB)

#new

$(COBJECTS) : %.o : %.c

$(CXXOBJECTS) : %.o : %.cpp

$(CCOBJECTS) : %.o : %.cc

%.o: %.c

$(CC) $(CFLAGS) -c $^ -o $@

%.o: %.cc

$(CXX) $(CFLAGS) $(CXXFLAGS) -c $^ -o $@

%.o: %.cpp

$(CXX) $(CFLAGS) $(CXXFLAGS) -c $^ -o $@

#new

%.o: %.cpp $(INCS) Makefile

@echo $(CFLAGS)

$(CXX) -c -o $@ $(CFLAGS) $<

$(LIB): $(OBJS) $(DEP) $(COBJECTS) $(CXXOBJECTS) $(CCOBJECTS) Makefile

@echo $(CFLAGS)

$(CXX) $(COBJECTS) $(CXXOBJECTS) $(CCOBJECTS) $(OBJS) -o $@ $(LIBS) $(LDFLAGS)

**Context/Use case:Integrating edge impulse with gstreamer library.
Specifically when I try to include CCObjects into my shared library it throws error.

tflite_detection_postprocess.o: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol _ZN6tflite18post_process_boxesE' which may bind externally can not be used when making a shared object tflite/linux-jetson-nano/libei_debug.a(libeitrt.cpp.o): In function readBuffer(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)’:
/workspace/TensorRT/ei/libeitrt.cpp:104:(.text+0x31c): dangerous relocation: unsupported relocation
/usr/bin/ld: tflite/linux-jetson-nano/libei_debug.a(libeitrt.cpp.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol _ZSt5fixedRSt8ios_base' which may bind externally can not be used when making a shared object; recompile with -fPIC tflite/linux-jetson-nano/libei_debug.a(libeitrt.cpp.o): In function EiTrt::reportOutput(samplesCommon::BufferManager const&, float*, int)’:
/workspace/TensorRT/ei/libeitrt.cpp:347:(.text+0x11b0): dangerous relocation: unsupported relocation
/usr/bin/ld: tflite/linux-jetson-nano/libei_debug.a(libeitrt.cpp.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `_ZTISt9bad_alloc’ which may bind externally can not be used when making a shared object; recompile with -fPIC

@kwasif2020

Thanks for reporting. We’re looking into it now.

1 Like

@kwasif2020 Just to confirm, you’re using example-standalone-linux repo, and you can build that without issue yes?

Can you paste the full makefile?

Yes, able to build the sample application code for camera. Given below is the entire make file. I have taken portions of JETSON_NANO flag from the sample makefile.

EI_SDK?=edge-impulse-sdk

PYTHON_CROSS_PATH?=

print-% : ; @echo $* = $($*)

UNAME_S := $(shell uname -s)

CFLAGS += -Wall -g -Wno-strict-aliasing

CFLAGS += -I.

CFLAGS += -Isource

CFLAGS += -Imodel-parameters

CFLAGS += -Itflite-model

CFLAGS += -Ithird_party/

CFLAGS += -Os

CFLAGS += -DNDEBUG

CFLAGS += -DEI_CLASSIFIER_ENABLE_DETECTION_POSTPROCESS_OP=1

CFLAGS += -g

CFLAGS += -Wno-asm-operand-widths

CXXFLAGS += -std=c++14

LDFLAGS += -lm -lstdc++

CFLAGS += -I/usr/local/include/opencv4/ -Iopencv/opencv/include -Iopencv/opencv/3rdparty/include -Iopencv/opencv/3rdparty/quirc/include -Iopencv/opencv/3rdparty/carotene/include -Iopencv/opencv/3rdparty/ittnotify/include -Iopencv/opencv/3rdparty/openvx/include -Iopencv/opencv/modules/video/include -Iopencv/opencv/modules/flann/include -Iopencv/opencv/modules/core/include -Iopencv/opencv/modules/stitching/include -Iopencv/opencv/modules/imgproc/include -Iopencv/opencv/modules/objdetect/include -Iopencv/opencv/modules/gapi/include -Iopencv/opencv/modules/world/include -Iopencv/opencv/modules/ml/include -Iopencv/opencv/modules/imgcodecs/include -Iopencv/opencv/modules/dnn/include -Iopencv/opencv/modules/dnn/src/vkcom/include -Iopencv/opencv/modules/dnn/src/ocl4dnn/include -Iopencv/opencv/modules/dnn/src/tengine4dnn/include -Iopencv/opencv/modules/videoio/include -Iopencv/opencv/modules/highgui/include -Iopencv/opencv/modules/features2d/include -Iopencv/opencv/modules/ts/include -Iopencv/opencv/modules/photo/include -Iopencv/opencv/modules/calib3d/include

ifeq ($(UNAME_S),Linux) # on Linux set the library paths as well

LDFLAGS += -Lopencv/lib -L/usr/local/lib -Wl,-R/usr/local/lib

endif

LDFLAGS += -lopencv_ml -lopencv_objdetect -lopencv_stitching -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core

CSOURCES = $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/TransformFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/CommonTables/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/BasicMathFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/ComplexMathFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/FastMathFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/SupportFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/MatrixFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/StatisticsFunctions/.c)

CXXSOURCES = $(wildcard tflite-model/.cpp) $(wildcard edge-impulse-sdk/dsp/kissfft/.cpp) $(wildcard edge-impulse-sdk/dsp/dct/.cpp) $(wildcard ./edge-impulse-sdk/dsp/memory.cpp) $(wildcard edge-impulse-sdk/porting/posix/.c*) $(wildcard edge-impulse-sdk/porting/mingw32/.c)

CCSOURCES =

CFLAGS += -DTF_LITE_DISABLE_X86_NEON=1

CSOURCES += edge-impulse-sdk/tensorflow/lite/c/common.c

CCSOURCES += $(wildcard edge-impulse-sdk/tensorflow/lite/kernels/.cc) $(wildcard edge-impulse-sdk/tensorflow/lite/kernels/internal/.cc) $(wildcard edge-impulse-sdk/tensorflow/lite/micro/kernels/.cc) $(wildcard edge-impulse-sdk/tensorflow/lite/micro/.cc) $(wildcard edge-impulse-sdk/tensorflow/lite/micro/memory_planner/.cc) $(wildcard edge-impulse-sdk/tensorflow/lite/core/api/.cc)

LDFLAGS += tflite/linux-jetson-nano/libei_debug.a -L/usr/local/cuda-10.2/targets/aarch64-linux/lib/ -lcudart -lnvinfer -lnvonnxparser -Wl,–warn-unresolved-symbols,–unresolved-symbols=ignore-in-shared-libs

LDFLAGS += -L/usr/local/lib -Wl,-R/usr/local/lib

COBJECTS := $(patsubst %.c,%.o,$(CSOURCES))

CXXOBJECTS := $(patsubst %.cpp,%.o,$(CXXSOURCES))

CCOBJECTS := $(patsubst %.cc,%.o,$(CCSOURCES))

WITH_OPENCV?=0

USE_OPTIMIZED_DSEXAMPLE?=0

CUDA_VER=10.2

ifeq ($(CUDA_VER),)

$(error “CUDA_VER is not set”)

endif

TARGET_DEVICE = $(shell gcc -dumpmachine | cut -f1 -d -)

CXX:= g++

ifeq ($(USE_OPTIMIZED_DSEXAMPLE),1)

SRCS:= gstdsexample_optimized.cpp

else

SRCS:= gstdsexample.cpp

endif

INCS:= $(wildcard *.h)

LIB:=libnvdsgst_dsexample.so

NVDS_VERSION:=6.0

DEP:=dsexample_lib/libdsexample.a

DEP_FILES:=$(wildcard dsexample_lib/dsexample_lib.* )

DEP_FILES-=$(DEP)

CFLAGS+= -fPIC -DDS_VERSION=“6.0.0” \

-I /usr/local/cuda-$(CUDA_VER)/include \

-I …/…/includes -I /opt/nvidia/deepstream/deepstream-6.0/sources/gst-plugins/gst-dsexample

GST_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/lib/gst-plugins/

LIB_INSTALL_DIR?=/opt/nvidia/deepstream/deepstream-$(NVDS_VERSION)/lib/

LIBS := -shared -Wl,-no-undefined \

-L dsexample_lib -ldsexample \

-L/usr/local/cuda-$(CUDA_VER)/lib64/ -lcudart -ldl \

-lnppc -lnppig -lnpps -lnppicc -lnppidei \

-L$(LIB_INSTALL_DIR) -lnvdsgst_helper -lnvdsgst_meta -lnvds_meta -lnvbufsurface -lnvbufsurftransform\

-Wl,-rpath,$(LIB_INSTALL_DIR)

OBJS:= $(SRCS:.cpp=.o)

PKGS:= gstreamer-1.0 gstreamer-base-1.0 gstreamer-video-1.0

ifeq ($(WITH_OPENCV),1)

CFLAGS+= -DWITH_OPENCV

ifeq ($(TARGET_DEVICE),aarch64)

PKGS+= opencv4

else

PKGS+= opencv

endif

endif

CFLAGS+=$(shell pkg-config --cflags $(PKGS))

LIBS+=$(shell pkg-config --libs $(PKGS))

all: $(LIB)

#all: runner

#.PHONY: runner clean

#$(LIB)

#new

$(COBJECTS) : %.o : %.c

$(CXXOBJECTS) : %.o : %.cpp

$(CCOBJECTS) : %.o : %.cc

%.o: %.c

$(CC) $(CFLAGS) -c $^ -o $@

%.o: %.cc

$(CXX) $(CFLAGS) $(CXXFLAGS) -c $^ -o $@

%.o: %.cpp

$(CXX) $(CFLAGS) $(CXXFLAGS) -c $^ -o $@

#new

%.o: %.cpp $(INCS) Makefile

@echo $(CFLAGS)

$(CXX) -c -o $@ $(CFLAGS) $<

$(LIB): $(OBJS) $(DEP) $(COBJECTS) $(CXXOBJECTS) $(CCOBJECTS) Makefile

@echo $(CFLAGS)

$(CXX) $(COBJECTS) $(CXXOBJECTS) $(CCOBJECTS) $(OBJS) -o $@ $(LIBS) $(LDFLAGS)

#$(LDFLAGS)

#$(CCOBJECTS)?? Gives error

$(DEP): $(DEP_FILES)

$(MAKE) -C dsexample_lib/

install: $(LIB)

cp -rv $(LIB) $(GST_INSTALL_DIR)

clean:

rm -rf $(OBJS) $(LIB)

I have tried to compile it with below Cmake, It compiles but gives undefined symbol error when I try to use the shared library. Can you suggest if I missed anything in my Cmake. Thanks

cmake_minimum_required(VERSION 3.13.1)

project(DSEdgeImpulse)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/libs)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(DEP_LIB ${CMAKE_SOURCE_DIR}/dsexample_lib/)
find_package(PkgConfig REQUIRED)
find_package(CUDA REQUIRED)
find_package(OpenCV REQUIRED)

pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0)
pkg_check_modules(GSTREAMER_BASE REQUIRED gstreamer-base-1.0)
pkg_check_modules(GSTREAMER_VIDEO REQUIRED gstreamer-video-1.0)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_FLAGS “-Wall -g -Wno-strict-aliasing -I. -Isource -Imodel-parameters -Itflite-model -Ithird_party/ -Os -DNDEBUG -DEI_CLASSIFIER_ENABLE_DETECTION_POSTPROCESS_OP=1 -g -Wno-asm-operand-widths -DTF_LITE_DISABLE_X86_NEON=1”)
message(CMAKE_C_FLAGS = ${CMAKE_C_FLAGS})
set(CMAKE_CXX_FLAGS “-std=c++14”)
message(CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${GSTREAMER_INCLUDE_DIRS})
include_directories(${CUDA_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(/opt/nvidia/deepstream/deepstream/sources/includes/)
include_directories(/usr/local/include/opencv4/)
include_directories(${DEP_LIB})
#include_directories(/opt/nvidia/deepstream/deepstream-6.0/sources/gst-plugins/gst-dsexample/tflite/linux-jetson-nano/)

link_directories(${GSTREAMER_LIBRARY_DIRS})
link_directories(${CUDA_LIBRARY_DIRS})
link_directories(${OPENCV_LIBRARY_DIRS})
link_directories(${DEP_LIB})
link_directories(/opt/nvidia/deepstream/deepstream/lib)

set(EI_SDK_FOLDER “${CMAKE_CURRENT_SOURCE_DIR}/edge-impulse-sdk”)

include(${EI_SDK_FOLDER}/cmake/utils.cmake)

#target_include_directories(app PRIVATE ${EI_SDK_FOLDER})

#RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}” “.cpp")
#RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}” "
.cc”)
#RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}” “*.c”)

#CSOURCES = $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/TransformFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/CommonTables/.c)
#$(wildcard edge-impulse-sdk/CMSIS/DSP/Source/BasicMathFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/ComplexMathFunctions/.c)
#$(wildcard edge-impulse-sdk/CMSIS/DSP/Source/FastMathFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/SupportFunctions/.c)
#$(wildcard edge-impulse-sdk/CMSIS/DSP/Source/MatrixFunctions/.c) $(wildcard edge-impulse-sdk/CMSIS/DSP/Source/StatisticsFunctions/.c)
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/DSP/Source/TransformFunctions” “.c")
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/DSP/Source/CommonTables” "
.c”)
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/DSP/Source/BasicMathFunctions” “.c")
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/DSP/Source/ComplexMathFunctions” "
.c”)
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/DSP/Source/FastMathFunctions” “.c")
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/DSP/Source/SupportFunctions” "
.c”)
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/DSP/Source/MatrixFunctions” “.c")
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/DSP/Source/StatisticsFunctions” "
.c”)

#check
RECURSIVE_FIND_FILE_APPEND(EI_SOURCE_FILES “${EI_SDK_FOLDER}/CMSIS/NN/Source” “*.c”)
#CSOURCES += edge-impulse-sdk/tensorflow/lite/c/common.c
LIST(APPEND EI_SOURCE_FILES “${EI_SDK_FOLDER}/tensorflow/lite/c/common.c”)

#CCSOURCES += $(wildcard edge-impulse-sdk/tensorflow/lite/kernels/.cc) $(wildcard edge-impulse-sdk/tensorflow/lite/kernels/internal/.cc)
#$(wildcard edge-impulse-sdk/tensorflow/lite/micro/kernels/.cc) $(wildcard edge-impulse-sdk/tensorflow/lite/micro/.cc)
#$(wildcard edge-impulse-sdk/tensorflow/lite/micro/memory_planner/.cc) $(wildcard edge-impulse-sdk/tensorflow/lite/core/api/.cc)

RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/tensorflow/lite/kernels” “.cc")
RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/tensorflow/lite/micro” "
.cc”)

RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/tensorflow/lite/micro/memory_planner” “.cc")
RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/tensorflow/lite/core/api” "
.cc”)

#CXXSOURCES = $(wildcard tflite-model/.cpp) $(wildcard edge-impulse-sdk/dsp/kissfft/.cpp)
#$(wildcard edge-impulse-sdk/dsp/dct/.cpp) $(wildcard ./edge-impulse-sdk/dsp/memory.cpp)
#$(wildcard edge-impulse-sdk/porting/posix/
.c*) $(wildcard edge-impulse-sdk/porting/mingw32/.c)
LIST(APPEND EI_SOURCE_FILES “tflite-model/onnx-trained.cpp”)
RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/dsp/kissfft” “.cpp")
RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/dsp/dct” "
.cpp”)
RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/dsp/dct” “.cpp")
LIST(APPEND EI_SOURCE_FILES “${EI_SDK_FOLDER}/dsp/memory.cpp”)
RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/porting/posix” "
.cpp”)
RECURSIVE_FIND_FILE_APPEND (EI_SOURCE_FILES “${EI_SDK_FOLDER}/porting/mingw32” “*.cpp”)

#target_sources(app PRIVATE ${EI_SOURCE_FILES})

add_library(nvdsgst_DSEdgeImpulse SHARED gstdsexample.cpp dsexample_lib/dsexample_lib.c ${EI_SOURCE_FILES} tflite/linux-jetson-nano/libei_debug.a)

target_link_libraries(nvdsgst_DSEdgeImpulse opencv_imgcodecs opencv_video opencv_imgproc opencv_core)
target_link_libraries(nvdsgst_DSEdgeImpulse ${CUDA_LIBRARIES})
target_link_libraries(nvdsgst_DSEdgeImpulse ${GSTREAMER_VIDEO_LIBRARIES})
target_link_libraries(nvdsgst_DSEdgeImpulse nvdsgst_helper nvdsgst_meta nvds_meta nvbufsurface nvbufsurftransform nvinfer nvonnxparser)

install(TARGETS nvdsgst_DSEdgeImpulse DESTINATION /opt/nvidia/deepstream/deepstream/lib/gst-plugins/)