「インストール - GCC」の版間の差分

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
ナビゲーションに移動 検索に移動
(ページの作成:「== 概要 == CentOSやSUSEのパッケージ管理システムで提供されているGCCのバージョンは古い可能性がある。<br> 最新のGCCでは、C++11…」)
 
42行目: 42行目:
<br>
<br>
最後に、PCを再起動する。<br>
最後に、PCを再起動する。<br>
<br><br>
== C++14 / C++17のサンプルコード ==
C++14では、ラムダ式のパラメータのタイプにautoが使用できる。<br>
<source lang="c++">
// sample.cpp
#include <iostream>
 
int main()
{
    std::cout << [](auto a, auto b) { return a + b; } (5, 6) << std::endl;
    std::cout << [](auto a, auto b) { return a + b; } (5.23, 6.45) << std::endl;
    return 0;
}
</source>
<br>
# 実行
g++-10.1 -Wall -pedantic sample.cpp -o sample
./sample
# 出力
11
11.68
<br>
このページの冒頭で述べたように、GCC 10.1は、C++17を完全にサポートしている。<br>
以下の例では、static_assertへのC++17変更の使用例をテストしている。<br>
<source lang="c++">
// sample.cpp
#include <type_traits>
#include <iostream>
 
struct A
{
    int foo;
};
struct B
{
    int foo = 0;
};
 
template <typename T>
void print(const T& a)
{
    static_assert(std::is_pod<T>::value);
    std::cout << a.foo << '\n';
}
 
int main()
{
    A x{1};
    B y{2};
    B z;
    print<A>(x);
    print<B>(y);
    print<B>(z);
    return 0;
}
</source>
<br>
コンパイルを行うには、-std=c++17オプションを付与する。<br>
また、上記のコードの17行目と24行目でトリガーされたコンパイルエラーが表示される。<br>
# 実行
g++-10.1 -std=c++17 -Wall -pedantic sample.cpp -o sample
# 出力
sample.cpp: In instantiation of ‘void print(const T&) [with T = B]’:
sample.cpp:24:13:  required from here
sample.cpp:14:33: error: static assertion failed
    17 |  static_assert(std::is_pod<T>::value);
<br>
もし、C++11の構文の詳細に興味がある場合は、Bjarne StroustrupによるC++プログラミング言語を読むことを推奨する。<br>
<br><br>
<br><br>


__FORCETOC__
__FORCETOC__
[[カテゴリ:CentOS]][[カテゴリ:SUSE]]
[[カテゴリ:CentOS]][[カテゴリ:SUSE]]

2020年6月19日 (金) 05:04時点における版

概要

CentOSやSUSEのパッケージ管理システムで提供されているGCCのバージョンは古い可能性がある。
最新のGCCでは、C++11からC++17を完全にサポートしており、また、C++20を部分的にサポートしている。
また、最新のGCCでは、C11およびC++14のサポートがデフォルトで有効になっている。(-std=c11または-std=c++14を追加する必要はない)


依存関係のライブラリのインストール

まず、システムが最新であることを確認する。

# CentOS
sudo yum update

# SUSE
sudo zypper update


以下の依存関係のライブラリをインストールする。

# CentOS
sudo yum install gmp-devel mpfr-devel libmpc-devel

# SUSE
sudo zypper install gmp-devel mpfr-devel mpc-devel



GCCのインストール

GCCの公式Webサイトにアクセスして、GCCのソースコードをダウンロードして解凍する。
ここでは、ホームディレクトリにインストールを行う。

mkdir -p ~/GCC/gcc-10_1_0 && cd ~/GCC
tar zxvf  -o src


以下のコマンドを実行して、GCCをビルドおよびインストールする。

./configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=$HOME/GCC/gcc-10_1_0 --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-10.1

make -j 8

make install-strip


インストールしたGCCへ環境変数のパスを通す。

vi ~/.profile

# .profileファイル
export export PATH=$HOME/GCC/gcc-10_1_0/bin:$PATH
export LD_LIBRARY_PATH=$HOME/GCC/gcc-10.1.0/lib64:$LD_LIBRARY_PATH


最後に、PCを再起動する。


C++14 / C++17のサンプルコード

C++14では、ラムダ式のパラメータのタイプにautoが使用できる。

 // sample.cpp
 
 #include <iostream>
  
 int main()
 {
    std::cout << [](auto a, auto b) { return a + b; } (5, 6) << std::endl;
    std::cout << [](auto a, auto b) { return a + b; } (5.23, 6.45) << std::endl;
    return 0;
 }


# 実行
g++-10.1 -Wall -pedantic sample.cpp -o sample
./sample

# 出力
11
11.68


このページの冒頭で述べたように、GCC 10.1は、C++17を完全にサポートしている。
以下の例では、static_assertへのC++17変更の使用例をテストしている。

 // sample.cpp
 
 #include <type_traits>
 #include <iostream>
  
 struct A
 {
    int foo;
 };
 
 struct B
 {
    int foo = 0;
 };
  
 template <typename T>
 void print(const T& a)
 {
    static_assert(std::is_pod<T>::value);
    std::cout << a.foo << '\n';
 }
  
 int main()
 {
    A x{1};
    B y{2};
    B z;
 
    print<A>(x);
    print<B>(y);
    print<B>(z);
 
    return 0;
 }


コンパイルを行うには、-std=c++17オプションを付与する。
また、上記のコードの17行目と24行目でトリガーされたコンパイルエラーが表示される。

# 実行
g++-10.1 -std=c++17 -Wall -pedantic sample.cpp -o sample

# 出力
sample.cpp: In instantiation of ‘void print(const T&) [with T = B]’:
sample.cpp:24:13:   required from here
sample.cpp:14:33: error: static assertion failed
    17 |   static_assert(std::is_pod<T>::value);


もし、C++11の構文の詳細に興味がある場合は、Bjarne StroustrupによるC++プログラミング言語を読むことを推奨する。