引入mimalloc头文件编译报错:std中无mi_realloc成员
使用mimalloc-override和mimalloc-new-delete编译报错的解决
我尝试在项目的common.hpp中通过包含mimalloc-new-delete.h和mimalloc-override.h来使用mimalloc,代码如下:
#pragma once #ifndef _common_hpp #define _common_hpp #define SOL_ALL_SAFETIES_ON 1 #include <mimalloc-new-delete.h> #include <mimalloc-override.h> extern "C" { #include <SDL2/SDL.h> #include <avif/avif.h> #include <curl/curl.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <physfs.h> } #include <algorithm> #include <iostream> #include <list> #include <map> #include <memory> #include <mutex> #include <string> #include <unordered_map> #include <fmt/core.h> #include <sol/sol.hpp> #include "constants.hpp" #include "deleters.hpp" #include "types.hpp" #endif
编译时出现如下错误:
-- Build files have been written to: /opt/workspace/carimbolabs/carimbo/build [ 4%] Building CXX object CMakeFiles/carimbo.dir/cmake_pch_arm64.hxx.pch In file included from <built-in>:1: In file included from /opt/workspace/carimbolabs/carimbo/build/CMakeFiles/carimbo.dir/cmake_pch_arm64.hxx:5: In file included from /opt/workspace/carimbolabs/carimbo/src/application.hpp:5: In file included from /opt/workspace/carimbolabs/carimbo/src/audiomanager.hpp:5: In file included from /opt/workspace/carimbolabs/carimbo/src/common.hpp:20: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:44: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:165: In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ostream:172: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/locale:2827:22: 错误:命名空间'std'中没有名为'mi_realloc'的成员;您是不是想直接使用'mi_realloc'? _Tp* __t = (_Tp*)std::realloc(__owns ? __b.get() : 0, __new_cap); ^~~~~ /Users/skhaz/.conan2/p/b/mimal0a19b82323bd4/p/include/mimalloc.h:112:40: 注:'mi_realloc'在此处声明 mi_decl_nodiscard mi_decl_export void* mi_realloc(void* p, size_t newsize) mi_attr_noexcept mi_attr_alloc_size(2); ^ 1 error generated. make[2]: *** [CMakeFiles/carimbo.dir/cmake_pch_arm64.hxx.pch] Error 1 make[1]: *** [CMakeFiles/carimbo.dir/all] Error 2 make: *** [all] Error 2
错误原因
mimalloc-override.h通过宏定义把全局的realloc替换成mi_realloc,但系统标准库的头文件(比如这里的<locale>)中存在std::realloc的调用,宏替换只会处理无命名空间前缀的realloc,导致编译时编译器试图查找std::mi_realloc,而这个符号并不存在。
解决办法
方法1:调整头文件包含顺序
把系统标准库的C++头文件(<iostream>、<algorithm>等)放在mimalloc的头文件之前包含,这样当mimalloc的替换宏生效时,系统标准库的代码已经完成预处理,不会被影响。修改后的common.hpp示例:
#pragma once #ifndef _common_hpp #define _common_hpp #define SOL_ALL_SAFETIES_ON 1 // 先包含系统C++标准库头文件 #include <algorithm> #include <iostream> #include <list> #include <map> #include <memory> #include <mutex> #include <string> #include <unordered_map> // 再包含mimalloc相关头文件 #include <mimalloc-new-delete.h> #include <mimalloc-override.h> extern "C" { #include <SDL2/SDL.h> #include <avif/avif.h> #include <curl/curl.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <physfs.h> } #include <fmt/core.h> #include <sol/sol.hpp> #include "constants.hpp" #include "deleters.hpp" #include "types.hpp" #endif
方法2:仅替换C++的new/delete(不覆盖C标准库分配函数)
如果不需要替换C标准库的malloc/realloc/free,可以只包含mimalloc-new-delete.h,同时定义MI_MALLOC_DISABLE_OVERRIDE宏来禁用mimalloc-override.h的替换功能:
#pragma once #ifndef _common_hpp #define _common_hpp #define SOL_ALL_SAFETIES_ON 1 #define MI_MALLOC_DISABLE_OVERRIDE 1 // 禁用C标准库分配函数的替换 #include <mimalloc-new-delete.h> // 不再包含mimalloc-override.h extern "C" { #include <SDL2/SDL.h> #include <avif/avif.h> #include <curl/curl.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <physfs.h> } #include <algorithm> #include <iostream> #include <list> #include <map> #include <memory> #include <mutex> #include <string> #include <unordered_map> #include <fmt/core.h> #include <sol/sol.hpp> #include "constants.hpp" #include "deleters.hpp" #include "types.hpp" #endif
内容的提问来源于stack exchange,提问作者Rodrigo
相关产品推荐
相关产品推荐

