Cygwin g++编译无法识别std::pmr命名空间问题排查
解决Cygwin g++ 11.3编译std::pmr::string时的"‘string’ is not a member of ‘std::pmr’"错误
问题现象
- Windows 10 64位系统下,使用Cygwin自带的g++ 11.3编译包含
std::pmr::string的代码时,抛出编译错误:‘string’ is not a member of ‘std::pmr’ - 相同代码在Godbolt平台上可以正常编译通过
原因分析
查看Cygwin环境中的<string>头文件可知,std::pmr命名空间下的相关类型(包括std::pmr::string)受到两个预编译宏的条件保护:
__cplusplus >= 201703L:必须启用C++17或更高版本标准_GLIBCXX_USE_CXX11_ABI:必须启用C++11 ABI
推测问题根源在于Cygwin自带的stdlib库配置异常,未默认满足上述两个宏的启用条件,导致std::pmr相关类型未被正确定义。
复现步骤
- 创建测试代码文件
test.cpp:
#include <string> #include <memory_resource> int main() { std::pmr::string pmr_str("test"); return 0; }
- 在Cygwin终端执行编译命令:
g++ -std=c++17 test.cpp -o test
- 触发编译错误:
‘string’ is not a member of ‘std::pmr’
可行替代方案
切换到MSYS环境,并添加静态编译参数,可正常编译生成可执行程序:
- 在MSYS终端执行编译命令:
g++ -std=c++17 -static test.cpp -o test
- 编译成功,生成的程序可正常运行
内容的提问来源于stack exchange,提问作者glades
相关产品推荐
相关产品推荐

