Replit中C++ chess-library编译报错求助(Python转国际象棋引擎)
解决C++国际象棋库编译报错问题
我之前用Python开发了一款国际象棋引擎,但Python跑Minimax高深度搜索速度太慢,转去Replit平台用C重写。Replit的Python环境自带chess库,我找了个C的chess-library库,克隆下来编译时出现大量报错,部分错误如下:
./chess-library/tests/../src/attacks_fwd.hpp:158:12: error: inline variables are a C++17 extension [-Werror,-Wc++17-extensions] static inline Magic BishopTable[64] = {}; ^ In file included from ./chess-library/tests/color.cpp:1: In file included from ./chess-library/tests/../src/include.hpp:35: In file included from ./chess-library/tests/../src/attacks.hpp:7: ./chess-library/tests/../src/piece.hpp:24:39: error: no type named 'string_view' in namespace 'std' constexpr explicit PieceType(std::string_view type) : pt(underlying::NONE) { ~~~~~^ ./chess-library/tests/../src/piece.hpp:140:26: error: no type named 'string_view' in namespace 'std' constexpr Piece(std::string_view p) : piece(underlying::NONE) { ~~~~~^ In file included from ./chess-library/tests/color.cpp:1: In file included from ./chess-library/tests/../src/include.hpp:35: In file included from ./chess-library/tests/../src/attacks.hpp:11: In file included from ./chess-library/tests/../src/board.hpp:14: ./chess-library/tests/../src/constants.hpp:5:16: error: nested namespace definition is a C++17 extension; define each namespace separately [-Werror,-Wc++17-extensions] namespace chess::constants { ^~~~~~~~~~~ { namespace constants In file included from ./chess-library/tests/color.cpp:1: In file included from ./chess-library/tests/../src/include.hpp:35: In file included from ./chess-library/tests/../src/attacks.hpp:11: In file included from ./chess-library/tests/../src/board.hpp:17: ./chess-library/tests/../src/utils.hpp:15:39: error: no member named 'string_view' in namespace 'std' [[nodiscard]] inline std::vector<std::string_view> splitString(std::string_view string, const char &delimiter) { ~~~~~^ ./chess-library/tests/../src/utils.hpp:15:81: error: unknown type name 'string'; did you mean 'std::string'? [[nodiscard]] inline std::vector<std::string_view> splitString(std::string_view string, const char &delimiter) { ^~~~~~ std::string /nix/store/1gf2flfqnpqbr1b4p4qz2f72y42bs56r-gcc-11.3.0/include/c++/11.3.0/bits/stringfwd.h:79:33: note: 'std::string' declared here typedef basic_string<char> string; ^
错误原因
所有报错都是因为编译器默认使用了低于C++17的标准,而这个库用到了多个C++17新增特性:
static inline静态变量初始化std::string_view字符串视图类型- 嵌套命名空间
chess::constants的定义方式
另外-Werror参数把这些C++17扩展的警告直接当成错误抛出,加剧了问题。
解决步骤
- 指定C++17编译标准:
- 如果用Makefile,修改
CXXFLAGS,添加-std=c++17,同时移除-Werror(或者保留但添加-Wno-c++17-extensions忽略相关警告)。修改后的配置类似:CXXFLAGS = -std=c++17 -O2 -Wall - 如果直接用g++命令编译,加上
-std=c++17参数:g++ -std=c++17 your_source_files.cpp -o chess_engine
- 如果用Makefile,修改
- 验证库的依赖要求:确认这个chess-library确实需要C17或更高版本,现代C库基本都会要求这个标准,按要求配置即可。
内容的提问来源于stack exchange,提问作者Benjamin Trumpy
相关产品推荐
相关产品推荐

