在OSX上包含来自另一个文件夹的标头失败

时间:2017-07-26 16:26:17

标签: c++ macos makefile include googletest

我有以下代码:

#include "gtest/gtest.h"
#include "Student_Info.hpp"
#include "../Chapter3/MedianCalculator.hpp"
#include "../Chapter3/FinalGradeCalculator.hpp"
#include <string>
#include <vector>
using std::vector;
using std::string;


float calculateFinalGradeFromStruct(const Student_info& s)
{
    // My code is here...
}

TEST(studentRecord, calculateGrades)
{
    // My tests are here...
}

以下Makefile:

GTEST_DIR=../googletest/googletest

CPPFLAGS += -isystem $(GTEST_DIR)/include
CXXFLAGS += -std=c++11 -g -Wall -Wextra -pthread
TESTS = app_unittest 
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

all : app_unittest $(TESTS)

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) $@ $^

*.o : *.cpp $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c *.cpp

app_unittest : *.o gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

gut: app_unittest
    ./app_unittest

如果我将MedianCalculator.hpp和FinalGradeCalculator.hpp移动到我上面有此代码的同一文件夹(并正确修复包含),一切正常。我正在使用MacOSX和XCode 8.3.3。

但是,当我尝试在示例中包含MedianCalculator.hpp和FinalGradeCalculator.hpp时(来自其他文件夹),我收到以下错误。

Risto-Mattis-MacBook-Air:Chapter4 ristomatti$ make gut
c++ -isystem ../googletest/googletest/include -std=c++11 -g -Wall -Wextra -pthread -c *.cpp
c++ -isystem ../googletest/googletest/include -I../googletest/googletest -std=c++11 -g -Wall -Wextra -pthread -c \
            ../googletest/googletest/src/gtest-all.cc
c++ -isystem ../googletest/googletest/include -I../googletest/googletest -std=c++11 -g -Wall -Wextra -pthread -c \
            ../googletest/googletest/src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
ar: creating archive gtest_main.a
a - gtest-all.o
a - gtest_main.o
c++ -isystem ../googletest/googletest/include -std=c++11 -g -Wall -Wextra -pthread -lpthread *.o gtest_main.a -o app_unittest
clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
Undefined symbols for architecture x86_64:
  "MedianCalculator::calculateMedian(std::__1::vector<int, std::__1::allocator<int> >)", referenced from:
      calculateFinalGradeFromStruct(Student_info const&) in 4-0.o
  "MedianCalculator::MedianCalculator()", referenced from:
      calculateFinalGradeFromStruct(Student_info const&) in 4-0.o
  "FinalGradeCalculator::calculateFinalGrades(std::__1::vector<float, std::__1::allocator<float> > const&)", referenced from:
      calculateFinalGradeFromStruct(Student_info const&) in 4-0.o
  "FinalGradeCalculator::FinalGradeCalculator()", referenced from:
      calculateFinalGradeFromStruct(Student_info const&) in 4-0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [app_unittest] Error 1

有人可以告诉我我的错误吗?

0 个答案:

没有答案