0

I have both c file and cpp file in project. Trying to build using g++. Now I can make gstdsexample.o and jetsonGPIO.o.

gstdsexample.o is object for cpp files and jetsonGPIO.o is object for c files.

In gstdsexample.cpp, I am using functions from c header file.

But the errors are those c functions used in cpp file have undefined reference errors.

gstdsexample.o: In function `gst_dsexample_start(_GstBaseTransform*)':
gstdsexample.cpp:(.text+0x1b3c): undefined reference to `gpioExport(unsigned int)'
gstdsexample.cpp:(.text+0x1b50): undefined reference to `gpioSetDirection(unsigned int, unsigned int)'
gstdsexample.o: In function `gst_dsexample_stop(_GstBaseTransform*)':
gstdsexample.cpp:(.text+0x238c): undefined reference to `gpioUnexport(unsigned int)'
gstdsexample.o: In function `gst_dsexample_transform_ip(_GstBaseTransform*, _GstBuffer*)':
gstdsexample.cpp:(.text+0x2db4): undefined reference to `gpioSetValue(unsigned int, unsigned int)'
gstdsexample.cpp:(.text+0x2dd4): undefined reference to `gpioSetValue(unsigned int, unsigned int)'
collect2: error: ld returned 1 exit status
Makefile:81: recipe for target 'libnvdsgst_dsexample.so' failed
make: *** [libnvdsgst_dsexample.so] Error 1

What is wrong with my Makefile?

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 
  C_SRCS:= jetsonGPIO.c
endif

INCS:= $(wildcard *.h)
LIB:=libnvdsgst_dsexample.so

NVDS_VERSION:=5.1

DEP:=dsexample_lib/libdsexample.a
DEP_FILES:=$(wildcard dsexample_lib/dsexample_lib.* )
DEP_FILES-=$(DEP)

CFLAGS+= -fPIC -DDS_VERSION=\"5.1.0\" \
     -I /usr/local/cuda-$(CUDA_VER)/include \
     -I ../../includes

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 -pthread -lnvds_osd\
    -L$(LIB_INSTALL_DIR) -lnvdsgst_helper -lnvdsgst_meta -lnvds_meta -lnvbufsurface -lnvbufsurftransform\
    -Wl,-rpath,$(LIB_INSTALL_DIR)

#OBJS:= $(SRCS:.cpp=.o)
OBJS:= $(SRCS:.cpp=.o) $(C_SRCS:.c=.o)
ifeq ($(TARGET_DEVICE),aarch64)
    PKGS:= gstreamer-1.0 gstreamer-base-1.0 gstreamer-video-1.0 opencv4
else
    PKGS:= gstreamer-1.0 gstreamer-base-1.0 gstreamer-video-1.0 opencv
endif

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

all: $(LIB)

%.o: %.cpp $(INCS) Makefile
    @echo $(CFLAGS)
    $(CXX) -c -o $@ $(CFLAGS) $< 


$(LIB): $(OBJS) $(DEP) Makefile
    @echo $(CFLAGS)
    $(CXX) -o $@  $(OBJS) $(LIBS)

$(DEP): $(DEP_FILES)
    $(MAKE) -C dsexample_lib/

install: $(LIB)
    cp -rv $(LIB) $(GST_INSTALL_DIR)

clean:
    rm -rf $(OBJS) $(LIB)

EDIT:

Undefined reference functions are from jetsonGPIO.c and jetsonGPIO.h I have another gstdsexample.cpp and gstdsexample.h. gstdsexample.cpp uses these functions from jetsonGPIO.h.

Now I can make jetsonGPIO.o.

OBJS:= $(SRCS:.cpp=.o) $(C_SRCS:.c=.o) create .o files.

ALL .o file are used to create lib in

$(LIB): $(OBJS) $(DEP) Makefile
        @echo $(CFLAGS)
        $(CXX) -o $@  $(OBJS) $(LIBS)
batuman
  • 7,066
  • 26
  • 107
  • 229
  • This is not a [mcve]. However, there is really only one possibility: you are not linking in an object file that contains the definitions for the symbols `gpioExport`, `gpioSetDirection`, or `gpioSetValue`. – Cody Gray - on strike Nov 11 '21 at 03:31
  • I have added EDIT. Basically `OBJS:= $(SRCS:.cpp=.o) $(C_SRCS:.c=.o)` produces .o files. Then they all are linked to make final $(LIB) – batuman Nov 11 '21 at 08:42
  • Yes, but do those object files contain definitions for the `gpio*` symbols? Or are those supposed to come from some existing library? – Cody Gray - on strike Nov 11 '21 at 08:50
  • Yes object file jetsonGPIO.o has all definitions. That is why I am confused. – batuman Nov 12 '21 at 00:59
  • If you're #including `jetsonGPIO.h` directly in a `*.cpp` file, then you likely need to wrap that inclusion in `extern "C"`. But it doesn't seem like that's the issue, because I don't see evidence in the error message that the linker is looking for mangled names. – Cody Gray - on strike Nov 12 '21 at 01:15
  • This is the sample I follow for GPIO. `https://github.com/jerrymhlee/jetsonTX2GPIO`. I don't see extern "C" – batuman Nov 12 '21 at 01:21
  • This protion in Makefile `%.o: %.cpp $(INCS) Makefile @echo $(CFLAGS) $(CXX) -c -o $@ $(CFLAGS) $< ` take cares to make object file from source gstdsexample.cpp and header files. All header files `INCS:= $(wildcard *.h)` in folder are linked together. Why functions in jetsonGPIO.h are undefined to gstdsexample.cpp/ – batuman Nov 12 '21 at 01:26
  • I copied the whole codes for GPIO to gstdsexample.cpp and gstdsexample.h. Problem solved. – batuman Nov 12 '21 at 02:11

0 Answers0