mercredi 26 juillet 2017

Including header from another folder fails on OSX

I have the following code:

#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...
}

And the following 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

If I move MedianCalculator.hpp and FinalGradeCalculator.hpp to same folder where I have this code above (and fix the includes properly), everything works just fine. I am using MacOSX and XCode 8.3.3.

However, when I try to include MedianCalculator.hpp and FinalGradeCalculator.hpp like in example (from the other folder), I get the following error.

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

Can someone please tell me what I do wrong?

Aucun commentaire:

Enregistrer un commentaire