使用 clang++ 编译时 CppUTest realloc 与 iostream 冲突

问题描述 投票:0回答:1

在我的 CPP 文件中我添加了

#include <iostream>

但是编译时出现以下错误:

/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

显然与std命名空间中的

realloc
文件中使用的宏
locale
有冲突:

__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;
}

并在

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__)

有人知道如何使用 clang++ 让 iostream 在 CppUTest 上工作吗?

我使用的是 Mac OS X M1 芯片,默认的 C++ 编译器是 clang++。

clang++ cpputest
1个回答
0
投票

确保您将正确的

-include
标志传递给 C 与 C++:

来自 CppUTest 文档

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

如果您尝试使用 C++ 中的 malloc 检测泄漏,则需要在任何 STL 标头之后

#include CppUTest/MemoryLeakDetectorMallocMacros.h

我遇到了类似的问题,我没有完全解决这个问题,但我相信它与您强调的 STL 冲突有关。 CppUTest 文档也在上面链接的内存泄漏部分讨论了这个问题。

这个问题也非常相似:错误:命名空间“std”中没有名为“mi_realloc”的成员;您的意思只是“mi_realloc”吗?

© www.soinside.com 2019 - 2024. All rights reserved.