プログラムはなぜ動くのか 第3版 知っておきたいプログラミングの基礎知識を読んでいてWin32向けCコンパイラ(BCC32)が用いられていた。そこで、以下を柱に記事を作成した。
1.BCC32のCコンパイラについて
2.Linux環境のCコンパイラ構築
3.調べる過程でわからなかった単語のメモ
1. BCC32
制限
- Win32向け開発
- Clangベースのコンパイラ
- C++11
- ライブラリのソースコードに制限あり
- 商用ライセンスは個人利用のみ
内容
- Embarcadero C++コンパイラ(bcc32c/bcc32x)
- Turboインクリメンタルリンカ(tlink32c)
- C++ Win32プリプロセッサ(cpp32)
- DLLからインポートライブラリを作成するImplibユーティリティ
- シンボルの大文字/小文字変換、拡張ライブラリの作成、ページサイズの変更などを行うtlibユーティリティ
- make、grep、touchなどの追加のコマンドラインツール
- Embarcadero C/C++ Runtime Library(RTL)、DinkumwareANSI/ISO Standard Template Library(STL)
2.Cコンパイラ ON Linux
環境は以下に記述。UbuntuにはCコンパイラとしてGCCが備わっているがBCC32がClangベースということなのでClang構築手順を残す。
環境
$ cat /etc/lsb-release
(略)
DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS"
$ arch
x86_64
要件
CMake公式サイトバージョン確認及びインストール
$cmake --version
コマンド 'cmake' が見つかりません。次の方法でインストールできます:
sudo snap install cmake # version 3.22.1, or
sudo apt install cmake # version 3.16.3-1ubuntu1
他のバージョンについては 'snap info cmake' を確認してください。
$sudo snap install cmake --classic
cmake 3.22.1 from Crascit✓ installed
Install cmake on Ubuntu$gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
$python3 --version
Python 3.8.10
$apt list --installed | grep zlib
zlib1g/focal-updates,now 1:1.2.11.dfsg-2ubuntu1.2 amd64 [インストール済み、自動]
$make --version
GNU Make 4.2.1
手順
$sudo apt-get install g++
$git clone --depth=1 https://github.com/llvm/llvm-project.git
$cd llvm-project
$mkdir build
$cd build
$sudo cmake -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" ../llvm
$sudo make
$vi ~/.profile
PATH="PATH:/usr/llvm/llvm-project/build/bin"
$source ~/.profile
$clang --version
clang version 14.0.0
エラー例
CMake Warning (dev) at /snap/cmake/1000/share/cmake-3.22/Modules/GNUInstallDirs.cmake:239 (message):
Unable to determine default CMAKE_INSTALL_LIBDIR directory because no
target architecture is known. Please enable at least one language before
including GNUInstallDirs.
Call Stack (most recent call first):
CMakeLists.txt:5 (include)
This warning is for project developers. Use -Wno-dev to suppress it.
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is unknown
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/cc
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:46 (project):
No CMAKE_CXX_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
あとはbcc32cをclangに読み替えるだけでプログラムはなぜ動くのか 第3版 知っておきたいプログラミングの基礎知識に対応できそう。しかし、全く同じというわけにはいかないのでClang 拡張 C++ コンパイラでサポートされていないオプションを読む必要が出てくるかもしれない。
[参考]
3.メモ
Win32
Clang
C++11
ユーティリティ
最後に
「公式の手順通りにやって上手くいかない」「BCC32以外はどんなコンパイラがあるのか」といった問いの一助になることを願う。
Cコンパイラに限らず勉強不足であった。