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

C++ 使用Makefile和GTest对函数的未定义引用,c++,makefile,googletest,C++,Makefile,Googletest,我是Makefiles新手,对函数的未定义引用有问题。目录结构如下所示: ├── linkedlist.cc ├── linkedlist.h │   ├── src │   │   ├── main.cc │   │   ├── removeduplicates.cc │   │   ├── removeduplicates_unittest.cc │   └── test │   ├── Makefile RemovedUpplicates.cc和RemovedUpplicates

我是Makefiles新手,对函数的未定义引用有问题。目录结构如下所示:

├── linkedlist.cc
├── linkedlist.h
│   ├── src
│   │   ├── main.cc
│   │   ├── removeduplicates.cc
│   │   ├── removeduplicates_unittest.cc
│   └── test
│       ├── Makefile
RemovedUpplicates.cc和RemovedUpplicates_unittest.cc从linkedlist.h导入声明,并从linkedlist.cc导入定义。RemovedUpplicates.cc还定义了在linkedlist.h中声明但未在linkedlist.cc中定义的新函数

使用GTest的示例Makefile文件取自repo中的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
我得到的错误(除其他类似错误外)是:


Makefile
的末尾,您拥有了所构建测试的依赖项。最后,在列出测试可执行文件
removeduplicates\u unittest
的地方,还需要列出目标文件
$(BASE\u 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
文件被链接到可执行文件中。

您似乎没有使用要测试的源(或对象)文件进行构建,然后是包含
linkedlist的实现的文件。
。此外,您显示的文件是普通的
Makefile
,不是CMake
CmakeLists.txt文件。您能告诉我问题出在哪里吗?哪一行需要修改?我把它称为CMake文件的错误,我将编辑帖子,只显示一个Makefile而不是CMake。谢谢你的建议。我试着做了这个修改,但是现在我得到了一个关于一些文件的多个定义的错误。你知道这有什么问题吗?@Razvan看来,
linkedlist.o
不是作为一个合适的对象文件构建的,而是一个可执行文件。您可能需要添加一个特定的目标来正确构建它。我建议你阅读一些关于
make
和makefile的教程。是的,这是个好主意。我将把你的答案标记为解决最初问题的解决方案。
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
#                                                                          Added this dependency
#                                                                          vvvvvvvvvvvvvvvvvvvvvvvv
removeduplicates_unittest : removeduplicates.o removeduplicates_unittest.o $(BASE_DIR)/linkedlist.o gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@