CppUTest realloc conflict with iostream when compiling with clang++

51 views Asked by At

In my CPP file I have added

#include <iostream>

But then I get the following error when I compile it:

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/locale:2827:22: error: no member named 'cpputest_realloc_location' in namespace 'std'; did you mean simply 'cpputest_realloc_location'?
    _Tp* __t = (_Tp*)std::realloc(__owns ? __b.get() : 0, __new_cap);
                     ^~~~~
/Users/NameLastname/Documents/Workspace/ControllerPlatform/UnitTestExample//../cpputest/include/CppUTest/MemoryLeakDetectorMallocMacros.h:26:14: note: 'cpputest_realloc_location' declared here
extern void* cpputest_realloc_location(void *, size_t, const char* file, size_t line);
             ^
1 error generated.
make: *** [/Users/NameLastname/Documents/Workspace/ControllerPlatform/UnitTestExample//../cpputest/build/MakefileWorker.mk:557: objs//Users/NameLastname/Documents/Workspace/ControllerPlatform/UnitTestExample//Encdr_UnitTest.o] Error 1

Apparently there is a conflict with the macros realloc which is used in the locale file in the std namespace:

__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
{
    bool __owns = __b.get_deleter() != __do_nothing;
    size_t __cur_cap = static_cast<size_t>(__e-__b.get()) * sizeof(_Tp);
    size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
                       2 * __cur_cap : numeric_limits<size_t>::max();
    if (__new_cap == 0)
        __new_cap = sizeof(_Tp);
    size_t __n_off = static_cast<size_t>(__n - __b.get());
    _Tp* __t = (_Tp*)std::realloc(__owns ? __b.get() : 0, __new_cap);
    if (__t == 0)
        __throw_bad_alloc();
    if (__owns)
        __b.release();
    __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
    __new_cap /= sizeof(_Tp);
    __n = __b.get() + __n_off;
    __e = __b.get() + __new_cap;
}

and in CppUTest/MemoryLeakDectectorMallocMacros.h

#define malloc(a) cpputest_malloc_location(a, __FILE__, __LINE__)
#define calloc(a, b) cpputest_calloc_location(a, b, __FILE__, __LINE__)
#define realloc(a, b) cpputest_realloc_location(a, b, __FILE__, __LINE__)
#define free(a) cpputest_free_location(a, __FILE__, __LINE__)

Anyone knows how to make iostream work on CppUTest with clang++ ?

I'm using Mac OS X M1 chip and the default c++ compiler is clang++.

1

There are 1 answers

0
ejohnso49 On

Ensure you are passing the correct -include flag to C vs C++:

From CppUTest's Docs:

CXXFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorNewMacros.h
CFLAGS += -include $(CPPUTEST_HOME)/include/CppUTest/MemoryLeakDetectorMallocMacros.h

and if you are trying to detect leaks with malloc from C++, you will need to #include CppUTest/MemoryLeakDetectorMallocMacros.h after any STL headers.

I have hit a similar issue, I have not root caused it completely but I believe it's related to STL conflicts as you have highlighted. The CppUTest docs also discuss this in the Memory Leak section linked above.

This question is very similar as well: error: no member named 'mi_realloc' in namespace 'std'; did you mean simply 'mi_realloc'?