0

I am trying to use GoogleTest to test a simple function, but as I run make in my build folder, the compiler throws Undefined Reference error messages at me for libusb library. I need to add libusb library in GTEST_SRCS_ part of the makefile but I get same error. I am new to unit testing so, I do not know what should I do? How to build gtest with libusb library?

Makefile

GTEST_DIR= /home/sahin/googletest/googletest
LIBUSB_DIR= /usr/include/libusb-1.0

USER_DIR=.
LIBS += -L/usr/lib -lusb-1.0 -L/usr/lib/x86_64-linux-gnu/libusb-1.0.so -L./ 

CPPFLAGS += -I$(GTEST_DIR)/include -I$(LIBUSB_DIR)

CXXFLAGS += -g -Wall -Wextra -pthread

OBJECTS = main.o test.o
TESTS = test

GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h
LIBUSB_HEADERS = $(LIBUSB_DIR)/*.h

all : $(TESTS)

clean :
    rm -f $(TESTS) gtest.a gtest_main.a *.o

GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

gtest-all.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
    $(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
    $(AR) $(ARFLAGS) $@ $^

test.o : $(USER_DIR)/test.cpp $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/test.cpp

test : test.o gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
  • Could the order of library paths and libraries to link be significant? Try putting the `-L` options before the `-l` option(s). – Andreas Aug 10 '22 at 09:46
  • You should also make sure to pass the directory having libusb-1.0.(a|so) as `-L` option in the `LIBS` variable. – Andreas Aug 10 '22 at 09:49
  • Use `-L` to specify the library search directory followed by the `-l` options. See https://www.gnu.org/software/make/manual/make.html for documentation. – Taimoor Zaeem Aug 10 '22 at 09:52
  • In your question please show the compiler command line that make invoked that generated the errors, and at least a few of the errors that you got (cut and paste the actual errors). Also we need to know the fully-qualified pathname to the library you are trying to link. – MadScientist Aug 10 '22 at 13:12
  • Does this answer your question? [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – 273K Aug 10 '22 at 13:55
  • The target `all` is wrong. It should not have actions, otherwise it compiles test.c, that is compiled by dependency targets `test` and `test.o`. – 273K Aug 10 '22 at 13:59
  • The action of the target `test` is expanded to `... -lusb-1.0 test.o gtest_main.a`. The order is important, should be `test.o gtest_main.a -lusb-1.0`. Update that target for the correct order of the linker arguments. – 273K Aug 10 '22 at 14:02

0 Answers0