我是Makefile的新手,对函数的未定义引用很麻烦。目录结构如下:
├── linkedlist.cc
├── linkedlist.h
│ ├── src
│ │ ├── main.cc
│ │ ├── removeduplicates.cc
│ │ ├── removeduplicates_unittest.cc
│ └── test
│ ├── Makefile
removeduplicates.cc和removeduplicates_unittest.cc导入来自linkedlist.h的声明和来自linkedlist.cc的定义。此外,replicateduplicates.cc定义了在linkedlist.h中声明但未在linkedlist.cc中定义的新函数。
使用GTest的示例Makefile文件取自仓库中的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
答案 0 :(得分:1)
在Makefile
的最后,您具有要构建的测试的依赖项。其中的最后一个,在其中列出测试可执行文件removeduplicates_unittest
的地方,还需要列出目标文件$(BASE_DIR)/linkedlist.o
:
# Added this dependency
# vvvvvvvvvvvvvvvvvvvvvvvv
removeduplicates_unittest : removeduplicates.o removeduplicates_unittest.o $(BASE_DIR)/linkedlist.o gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
这将导致linkedlist.o
文件链接到可执行文件中。