我一直在努力将我们的代码库迁移到 C++17,现在已经可以在本地使用 g++ 和 clang。
但是,我在使用 Clang 6.0 和 Clang 5.0 的 Travis 上遇到编译失败,这两个版本据说都支持 C++17。
此外,在我的机器上使用 Clang 6.0 编译相同的代码成功。
失败的相关代码是通过大括号初始化返回一个元组,例如:
return {str.str(), key};
我收到的具体错误信息是:
/home/travis/build/turtlecoin/turtlecoin/src/Mnemonics/Mnemonics.cpp:51:20: error: chosen constructor is explicit in copy-initialization
return {str.str(), key};
^~~~~~~~~~~~~~~~ /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/tuple:539:19: note: explicit constructor declared here
constexpr tuple(_U1&& __a1, _U2&& __a2)
它似乎按预期使用了 clang-6.0:
-- Check for working C compiler: /usr/bin/clang-6.0
-- Check for working C compiler: /usr/bin/clang-6.0 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/clang++-6.0
-- Check for working CXX compiler: /usr/bin/clang++-6.0 -- works
我的本地 clang 运行良好:
clang version 6.0.1 (tags/RELEASE_601/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
这是 Travis 的问题吗?铛?我的代码?
如果有帮助,完整的构建日志在这里: https://travis-ci.org/turtlecoin/turtlecoin/jobs/437857481
编辑:忘记提及 - 我在我的 CMakeLists 中使用标志启用 C++17 模式:
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
谢谢。
最佳答案
正如@Yakk - Adam Nevraumont 所说,这是由于 libstdc++ 不是最新的。我们可以通过安装更新的版本来解决这个问题。
由于 Travis 使用 Ubuntu 14.04,我们可以从 ubuntu-toolchain-r-test
PPA 中获取 libstdc++-7-dev
。
在你的 travis 配置中:
- os: linux
compiler: clang
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-6.0
packages:
- clang-6.0
- libstdc++-7-dev
env:
- MATRIX_EVAL="CC=clang-6.0 && CXX=clang++-6.0"
关于C++17 代码无法使用 Clang-6.0 在 Travis 上编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52674349/