使用 Makefile 和 GTest 对函数的未定义引用

Undefined reference to function using a Makefile and GTest

我是 Makefile 的新手,我在处理未定义的函数引用时遇到了麻烦。目录结构如下:

├── linkedlist.cc
├── linkedlist.h
│   ├── src
│   │   ├── main.cc
│   │   ├── removeduplicates.cc
│   │   ├── removeduplicates_unittest.cc
│   └── test
│       ├── Makefile

removeduplicates.cc 和 removeduplicates_unittest.cc 从 linkedlist.h 导入声明,从 linkedlist.cc 导入定义。 removeduplicates.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>)'

有人知道我做错了什么吗?

EDIT1:我遵循了@Some programmer dude 给出的建议,但现在我有多次定义的函数:

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

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 文件被链接到可执行文件中。