使用 CMake C++17 项目设置 Travis CI

Setting up Travis CI with CMake C++17 project

本文关键字:Travis CI 设置 项目 CMake C++17 使用      更新时间:2023-10-16

我正在尝试使用C++17项目设置Travis CI。我们使用CLion进行开发,这意味着我们有一个自动生成的CMakeLists.txt文件。我是从命令行编译和运行C++的新手。

据我了解,特拉维斯使用的所有内容都已经过时,需要更新才能与 C++17 一起使用。到目前为止,我的 .travis.yml 看起来像这样,我相信它有很多问题。

dist: trusty
sudo: require
language: cpp
compiler: gcc
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- gcc-6
- g++-6
- cmake
script:
- sudo ln -s /usr/bin/gcc-6 /usr/local/bin/gcc
- sudo ln -s /usr/bin/g++-6 /usr/local/bin/g++
- export CC=/usr/bin/gcc-6
- export CXX=/usr/bin/g++-6
- cmake --version
- cmake CMakeLists.txt
- cmake  --build . --target neat

生成失败,因为无法导入 C++17 库之一。

编辑添加了特拉维斯错误消息。

$ git clone --depth=50 --branch=feature/travis-ci https://github.com/sheldonkwoodward/bNEAT.git sheldonkwoodward/bNEAT
Cloning into 'sheldonkwoodward/bNEAT'...
remote: Counting objects: 778, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 778 (delta 9), reused 13 (delta 5), pack-reused 759
Receiving objects: 100% (778/778), 2.62 MiB | 12.71 MiB/s, done.
Resolving deltas: 100% (470/470), done.
$ cd sheldonkwoodward/bNEAT
$ git checkout -qf e1827e4ca53b041322e92dc207f1e133968a69f8
$ export CXX=g++
$ export CC=gcc
$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0.01s$ sudo ln -s /usr/bin/gcc-6 /usr/local/bin/gcc
The command "sudo ln -s /usr/bin/gcc-6 /usr/local/bin/gcc" exited with 0.
0.01s$ sudo ln -s /usr/bin/g++-6 /usr/local/bin/g++
The command "sudo ln -s /usr/bin/g++-6 /usr/local/bin/g++" exited with 0.
0.00s$ export CC=/usr/bin/gcc-6
The command "export CC=/usr/bin/gcc-6" exited with 0.
0.00s$ export CXX=/usr/bin/g++-6
The command "export CXX=/usr/bin/g++-6" exited with 0.
0.13s$ cmake --version
cmake version 3.9.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
The command "cmake --version" exited with 0.
0.91s$ cmake CMakeLists.txt
-- The C compiler identification is GNU 6.4.0
-- The CXX compiler identification is GNU 6.4.0
-- Check for working C compiler: /usr/bin/gcc-6
-- Check for working C compiler: /usr/bin/gcc-6 -- 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/g++-6
-- Check for working CXX compiler: /usr/bin/g++-6 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/travis/build/sheldonkwoodward/bNEAT
The command "cmake CMakeLists.txt" exited with 0.
0.15s$ cmake  --build . --target neat
Scanning dependencies of target neat
[ 11%] Building CXX object CMakeFiles/neat.dir/main.cpp.o
/home/travis/build/sheldonkwoodward/bNEAT/src/SnakeGame.hpp:9:20: fatal error: optional: No such file or directory
#include <optional>
^
compilation terminated.
make[3]: *** [CMakeFiles/neat.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/neat.dir/all] Error 2
make[1]: *** [CMakeFiles/neat.dir/rule] Error 2
make: *** [neat] Error 2
The command "cmake  --build . --target neat" exited with 2.
Done. Your build exited with 1.

CMakeList.txt

cmake_minimum_required(VERSION 3.8)
project(neat)
set(CMAKE_CXX_STANDARD 17)

set(SOURCE_FILES main.cpp src/ANN.cpp src/ANN.hpp src/ConnectionGene.cpp src/ConnectionGene.hpp src/Gene.cpp src/Gene.h src/Node.cpp src/Node.hpp src/NEAT.cpp src/NEAT.hpp src/SnakeGame.cpp src/SnakeGame.hpp src/Snake.cpp src/Snake.hpp)

# Tests
enable_testing()
add_subdirectory(tests)
add_executable(neat ${SOURCE_FILES})

解决方案是使用 gcc-7 和 g++-7 来添加对 std::optional 的支持。我的工作.travis.yml:

language: cpp
compiler: gcc
os: linux
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-7
script:
- export CC=gcc-7
- export CXX=g++-7
- cmake --version
- cmake CMakeLists.txt
- cmake  --build . --target neat