Я новичок в Makefiles, и у меня возникли проблемы с неопределенной ссылкой на функцию. Структура каталогов следующая:
├── linkedlist.cc
├── linkedlist.h
│ ├── src
│ │ ├── main.cc
│ │ ├── removeduplicates.cc
│ │ ├── removeduplicates_unittest.cc
│ └── test
│ ├── Makefile
removeduplicates.cc и removeduplicates_unittest.cc импортируют объявления из connectedlist.h и определения из connectedlist.cc. Также removeduplicates.cc определяет новые функции, которые объявлены в connectedlist.h, но не определены в connectedlist.cc.
Пример файла Makefile, который использует GTest, взят из примера GTest в репозитории и выглядит следующим образом. Изменяется только для включения файлов выше.
# A sample Makefile for building Google Test and using it in user
# tests. Please tweak it to suit your environment and project. You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
# make [all] - makes everything.
# make TARGET - makes the given target.
# make clean - removes all files generated by make.
# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.
# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = ../../../GMOCK_ROOT/GTEST_DIR/googletest/
# Where to find user code.
USER_DIR = ../src
BASE_DIR = ../..
# Flags passed to the preprocessor.
# Set Google Test's header directory as a system directory, such that
# the compiler doesn't generate warnings in Google Test headers.
CPPFLAGS += -isystem $(GTEST_DIR)/include
# Flags passed to the C++ compiler.
CXXFLAGS += -I.. -std=c++17 -g -Wall -Wextra -pthread
# All tests produced by this Makefile. Remember to add new tests you
# created to the list.
TESTS = removeduplicates_unittest
# All Google Test headers. Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
$(GTEST_DIR)/include/gtest/internal/*.h
# House-keeping build targets.
all : $(TESTS)
clean :
rm -f $(TESTS) gtest.a gtest_main.a *.o
# Builds gtest.a and gtest_main.a.
# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized. This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
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) $@ $^
# Builds a sample test. A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.
removeduplicates.o : $(USER_DIR)/removeduplicates.cc $(BASE_DIR)/linkedlist.h $(BASE_DIR)/linkedlist.cc $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(BASE_DIR)/linkedlist.cc $(USER_DIR)/removeduplicates.cc
removeduplicates_unittest.o : $(USER_DIR)/removeduplicates_unittest.cc $(BASE_DIR)/linkedlist.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/removeduplicates_unittest.cc
removeduplicates_unittest : removeduplicates.o removeduplicates_unittest.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
Я думаю, что ошибка исходит из этой строки, но я не уверен, как ее изменить:
CXXFLAGS += -I.. -std=c++17 -g -Wall -Wextra -pthread
Ошибка, которую я получаю (среди прочих похожих):
/RemoveDuplicates/test/../src/removeduplicates_unittest.cc:10: undefined reference to `LinkedList::LinkedList(std::initializer_list<int>)'
Кто-нибудь знает, что я делаю не так?
РЕДАКТИРОВАТЬ1: Я последовал предложению @Some программиста, чувак, но теперь у меня есть функции, которые определены несколько раз:
g++ -isystem ../../../GMOCK_ROOT/GTEST_DIR/googletest//include -I.. -std=c++17 -g -Wall -Wextra -pthread -lpthread removeduplicates.o removeduplicates_unittest.o ../../linkedlist.o gtest_main.a -o removeduplicates_unittest
../../linkedlist.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
../../linkedlist.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
../../linkedlist.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
../../linkedlist.o: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
../../linkedlist.o: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o:(.data+0x0): first defined here
../../linkedlist.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
../../linkedlist.o:(.data+0x10): first defined here
/usr/bin/ld: error in ../../linkedlist.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
Makefile:82: recipe for target 'removeduplicates_unittest' failed
make: *** [removeduplicates_unittest] Error 1