Eigen with CPPUTest? - c++

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

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.

Related

SFML library doesn't link with makefile compilation

I have been trying to link the SFML dlls to my windows C++ project, but I can't get it to work. I always end up with:
fatal error: SFML/System.hpp: No such file or directory
I've tried a bunch of things but nothing changes the issue.
Here is my makefile:
PROGRAM = zero_flip
OBJS = src/main.o src/Math.o src/card.o src/game_board.o src/indicator.o src/ui.o
CXX = g++
CXX_FLAGS = -O0 -g -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable
LIB_DIRS = -L./Resources/libs/
LIBS = -lsfml-system -lsfml-graphics -lsfml-window -lsfml-audio
LNK_FLAGS = $(LIB_DIRS) $(LIBS)
DEPS=$(OBJS:.o=.d)
.PHONY: all clean
all: $(PROGRAM)
-include $(DEPS)
%.o: %.cpp
$(CXX) $(CXX_FLAGS) $(LNK_FLAGS) $< -o $#
$(PROGRAM): $(OBJS)
$(CXX) $(CXX_FLAGS) $(LNK_FLAGS) $^ -o $#
clean:
rm -f $(OBJS) $(DEPS) $(PROGRAM) && clear
The "./Resources/libs/" directory contains:
openal32.dll
sfml-audio-2.dll
sfml-audio-d-2.dll
sfml-graphics-2.dll
sfml-graphics-d-2.dll
sfml-system-2.dll
sfml-system-d-2.dll
sfml-window-2.dll
sfml-window-d-2.dll
Can anyone get me unstuck please this is driving me mad.
This is wrong:
%.o: %.cpp
$(CXX) $(CXX_FLAGS) $(LNK_FLAGS) $< -o $#
This rule says it will compile a source file into an object file, but the recipe actually builds a complete executable: it will compile the source file like xxx.cpp then link it into a program named xxx.o. You need to invoke just the compiler here, not the linker, so you should not have $(LNK_FLAGS) and you need to add the -c option to tell the compiler to stop after compiling and not link.
Then you need to add an -I option to the compile line telling the compiler where to find the header files needed during compilation... in this case SFML/System.hpp.

Makefile does not evaluate OBJECTS variable

I am trying to build a Makefile that will build a shared library with g++ and I find that it is not evaluating the OBJECTS variable. This is on Ubuntu 18.04 and all the files are in the same current directory. Secondly it is completely skipping the source file compilation and proceeding directly to evaluate the linking instruction. As a clarification I am using GNU Make 4.1
Here is what I get when I type make all
g++ -shared -pthread -o tree.so
g++: fatal error: no input files
compilation terminated.
Makefile:12: recipe for target 'tree.so' failed
make: *** [tree.so] Error 1
Here is my Makefile code
CC=g++
CFLAGS = -I/usr/local/include -Wall -std=c++17 -O3 -march=native -Ofast -ftree-vectorize
LIBS=-shared -pthread
SOURCES=$(wildcard *.cpp)
OBJECTS=$(wildcard *.o)
TARGET=tree.so
all:$(TARGET)
$(TARGET) : $(OBJECTS)
$(CC) $(LIBS) -o $(OBJECTS) $(TARGET)
$(OBJECTS):$(SOURCES)
$(CC) -c -g $(CFLAGS) $(SOURCES)
clean:
rm -f $(OBJECTS) $(TARGET)
If you only have the *.cpp files in your directories, then there is not any *.o yet, so your $(wildcard *.o) will expand to nothing.
What you want is to get the *.cpp files and compute the corresponding *.o files:
OBJECTS=$(patsubst %.cpp,%.o,$(SOURCES))
or equivalently:
OBJECTS=$(SOURCES:.cpp=.o)
Now, your compiler command is not the best one, because if you touch any source file all will be compiled. You can use instead:
$(OBJECTS): %.o: %.cpp
$(CC) -c -g $(CFLAGS) $< -o $#
So that only the touched files are actually rebuilt.
Also you have the linking command wrong. It should be:
$(TARGET) : $(OBJECTS)
$(CC) $(LIBS) -o $(TARGET) $(OBJECTS)
because the argument to the -o option is the output file, that is the target.

Crosscompile for OpenWRT with c++11 flag

I am new in OpenWRT and I need to crosscompile c++ program. The problem is, I am still getting this error:
error: 'to_string' is not a member of 'std'
"duplicate raw value " + std::to_string(it->first));
^
Now I know, it's problem, that there isn't used c++11 flag during compilation. I tried to add TARGET_CXXFLAGS into Makefile, but it didn't help me.
Here is the Makefile:
include $(TOPDIR)/rules.mk
PKG_NAME:=gateway
PKG_VERSION:=1.0
PKG_RELEASE:=2
INIT_SCRIPT_NAME:=$(PKG_NAME)
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
PKG_SOURCE_PROTO:=git
PKG_BUILD_DEPENDS:=+libmosquittopp +poco +zmq +cppzmq +openzwave +cppunit +bluez-libs
PKG_SOURCE_URL:=....
PKG_SOURCE_VERSION:=...
CMAKE_INSTALL:=1
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
CMAKE_OPTIONS += \
-DCMAKE_INSTALL_PREFIX=/ \
-DCMAKE_BUILD_TYPE:STRING=Release
TARGET_CXXFLAGS += -DPOCO_NO_FPENVIRONMENT
TARGET_CXXFLAGS += -g -std=c++11 -Wall -pedantic
....
Thank you for your advice, Jakub.
Depends on GCC version and STD lib version. I can confirm that the last version from upstream works perfectly. Here is my test Makefile which may help you and give you some clues.
CXX=/media/build/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-5.5.0_musl_eabi/bin/arm-openwrt-linux-g++
RM=rm -f
LDLIBS=
CPPFLAGS=-Wall -std=c++11
LDFLAGS=-L/media/build/openwrt/staging_dir/target-arm_cortex-a7+neon-vfpv4_musl_eabi/usr/lib
INC=-I. -I/media/build/openwrt/staging_dir/target-arm_cortex-a7+neon-vfpv4_musl_eabi/usr/include
SOURCES=main.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE = hello
all: $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CXX) -o $# $^ $(LDFLAGS) $(LDLIBS)
.cpp.o:
$(CXX) $(INC) $(CPPFLAGS) -c $<
clean:
$(RM) $(OBJECTS)
distclean: clean
$(RM) $(EXECUTABLE)
.PHONY: all clean distclean

error: boost/regex.hpp: No such file or directory

I am trying to compile my C++ code using the GNU compiler under Linux using the following Makefile
CXX=gcc #icpc
RM=rm -f
CPPFLAGS=-g -O3 -fopenmp
CFLAGS= -Wall -c
OPENMP = -fopenmp
BIN = theVeecode_$(CXX)
LIBS= -L /path-to-boost/boost_1_53_0/stage/lib/ -lboost_regex
CPPSRCS=mathtools.cpp time_.cpp read_input.cpp vee_ao_calc.cpp vee_mo_calc.cpp write_int2e.cpp memory_check.cpp
OBJS=$(subst .cpp,.o,$(CPPSRCS))
OBJS+=$(COBJS)
all: $(BIN)
$(BIN): $(OBJS)
$(CXX) main.cpp $(OPENMP) -o $(BIN) $(OBJS) $(LIBS)
clean:
$(RM) $(OBJS) $(BIN)
dist-clean: clean
$(RM) $(BIN)
When I run the make command, I get the following error messages:
gcc -g -O3 -fopenmp -c -o read_input.o read_input.cpp
read_input.cpp:9:27: error: boost/regex.hpp: No such file or directory
read_input.cpp: In function 'void input::read_n_occ()':
read_input.cpp:95: error: 'boost' has not been declared
read_input.cpp:95: error: 'regex_search' was not declared in this scope
make: *** [read_input.o] Error 1
The read_input.cpp file starts with
#... // other includes
#include <boost/regex.hpp>
using namespace std;
namespace xxx
{
//some code here
}
The library path "/path-to-boost/boost_1_53_0/stage/lib/" contains the files
libboost_regex.a, libboost_regex.so and libboost_regex.so.1.53.0.
I don't understand why the compiler doesn't find the library files. Does anyone have any ideas why it's not working and how to fix it?
Thanks in advance.
As it turned out, the problem was in the Makefile. More specifically, the path to the boost library was not included during the compilation of the .cpp files using boost. Fixed it by adding the library explicitly in the compilation step:
%.o: %.cpp $(DEPS)
$(CXX) -c -o $# $< $(CPPFLAGS) $(LIBS)
Finally, the Makefile is as follows:
CXX=gcc #icpc
RM=rm -f
CPPFLAGS=-g -O3 -fopenmp
OPENMP = -fopenmp
BIN = theVeecode_$(CXX)
LIBS= -I /path-to-boost/boost_1_53_0/
LIBS+= -L /path-to-boost/boost_1_53_0/stage/lib/ -lboost_regex
CPPSRCS=mathtools.cpp time_.cpp read_input.cpp vee_ao_calc.cpp vee_mo_calc.cpp write_int2e.cpp memory_check.cpp
OBJS=$(subst .cpp,.o,$(CPPSRCS))
DEPS=Vector3.h mathtools.h memory_check.h read_input.h time_.h vee_ao_calc.h vee_mo_calc.h write_int2e.h
%.o: %.cpp $(DEPS)
$(CXX) -c -o $# $< $(CPPFLAGS) $(LIBS)
$(BIN): $(OBJS)
$(CXX) main.cpp $(OPENMP) -o $(BIN) $(OBJS) $(LIBS)
clean:
$(RM) $(OBJS) $(BIN)
dist-clean: clean
$(RM) $(BIN)
To get the word out there;
For my case, I was missing the libboost-dev package.
On debian, you can install it with sudo apt install libboost-dev

Confused how my Makefile is remaking object files

My make file is failing to find my include directory when it tries to remake object files. For example, when I call make tests I get the output:
g++ -c -o sdl_class.o sdl_class.cpp
sdl_class.cpp:9:23: fatal error: sdl_class.h: No such file or directory
#include <sdl_class.h>
^
compilation terminated.
make: *** [sdl_class.o] Error 1
My Makefile is this:
#Originally from: http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/
#But will be heavily modified
IDIR =../include
CC=g++
CFLAGS=-w -I$(IDIR)
#ODIR=obj
LDIR =../lib
LIBS=-lSDL2
_DEPS = sdl_class.h SDL_image.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
OBJ = sdl_class.o tests.o
#OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
%.o: %.cpp $(DEPS)
$(CC) -c -o $# $< $(CFLAGS) $(LIBS)
tests: sdl_class.o tests.o
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
all: $(OBJ)
$(CC) -o $# $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f *.o *~ core $(IDIR)/*~
My understanding is that when I call make tests, that it should attempt to remake the sdl_class.o file. This should then call the %.o rule, which should try to make the object file by calling something like:
g++ -c -o sdl_class.o sdl_class.cpp -w -I../include -lSDL2
However, this is not the case as it looks like it is calling $(CC) -c -o $# $< $(CFLAGS) $(LIBS), as you can see from above.
Do I have a fundamental misunderstanding about how make builds its rules? Seems likely, this is my first Makefile. Perhaps I am confused on how compilation works in general, as I'm somewhat new to that as well.
I would say that the problem is that one or more of the files ../include/sdl_class.h or ../include/SDL_image.h does not exist. Because of that, make is deciding that your pattern rule does not match (because not all the prerequisites can be found or made) and it defaults to the built-in rule to create object files from .cpp files.
The built-in rules use the make variables CXX for the C++ compiler and CXXFLAGS for the C++ flags: the CC and CFLAGS variables are used for the C compiler. That's why your settings for CFLAGS are being ignored.
If you run make -d sdl_class.o you'll see which file make is looking for and why it decides to not use your pattern rule.
If you rewrite your rules like this it will work better:
%.o: %.cpp
$(CC) -c -o $# $< $(CFLAGS)
sdl_class.o tests.o: $(DEPS)
because make will now complain that the relevant files can't be found or created.
There are other issues, of course. You shouldn't be passing $(LIBS) to your compile command; that belongs only in your link line. And, you should probably stick to the standard variables CXX for the C++ compiler, CPPFLAGS for preprocessor flags like -I and -D, and CXXFLAGS for C++ compiler flags. Also, linker library flags like -L../lib go in LDFLAGS and linker libraries like -lSDL2 go in LDLIBS.
CC/CCFLAGS are for C compilation. You should use CXX and CXXFLAGS for C++. They are used in built-in rules and in the LINK.cc macro, making the Makefile much simpler, and thus less error prone.
CXXFLAGS = -Wall ...
prog : foo.o bar.o
$(LINK.cc) -o $# $^
see Default linker setting in Makefile for linking C++ object files