Search code examples
c++compiler-errorsg++eigencpputest

Eigen with CPPUTest?


I am having a problem when trying to use CPPUTest to test my library.

Everything was fine until i included Eigen library to handle matrix processing. When i tried to build with g++, Eigen library kept throwing errors:

  • /eigen3/Eigen/src/Core/util/Memory.h:270:41 error: 'ptr' does not name a type

  • /eigen3/Eigen/src/Core/CoreEvaluators.h:1655:12 error: expected type-specifier before 'static_cast'

  • /eigen3/Eigen/src/Core/PlainOBjectBase.h:137:5 error: declaration of 'operator new' as non-function

If Eigen or CPPUTest runs separately, no error is output.

My guess is the two libraries have conflicts at some point.

Really need some helps here. Big thanks.

Edit 1: This is my Makefile:

CXX = g++ -std=c++0x -lstdc++ CXXFLAGS = -g -Wall -static -fprofile-arcs -ftest-coverage -I./ -I$(CPPUTEST_HOME)/include LDFLAGS = -L./ -L$(CPPUTEST_HOME)/lib -lCppUTest -lCppUTestExt -pthread CPPUTEST_HOME = ./cpputest/workspace/install USER_CFLAGS = -I /usr/local/include/eigen3 TARGET = MyLibrary SRCS = MyLibrary.cpp MyLibraryTest.cpp OBJS = $(SRCS:.cpp=.o) all: $(TARGET) $(TARGET): $(OBJS) $(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS) $(OBJS): $(SRCS) $(CXX) -c $(CXXFLAGS) $^ %.o: %.cpp $(CXX) -c $(CXXFLAGS) $<
.PHONY: clean clean: rm -f $(TARGET) $(OBJS) *.gcno *.gcov ~ find . -name ".gcda" | xargs -r r


Solution

  • It appears that CppUTest defines a macro new: https://github.com/cpputest/cpputest/blob/master/include/CppUTest/MemoryLeakDetectorNewMacros.h#L76

    When I #include <Eigen/Core> before #include <CppUTest/TestHarness.h>, I don't get the error you reported (did no further testing, though). Alternatively, you can #undef new after including CppUTest or define CPPUTEST_MEM_LEAK_DETECTION_DISABLED before including CppUTest (that will of course disable leak detection).

    The offending line in Eigen is using the placement-new operator (i.e., it does not allocate memory itself), and it's syntax is what throws CppUTest's new macro off.