自己投資としてチャレンジしている内容を Blog で公開しています。今回は Java ではなく C++ を Visual Studio Code 上で実装したいと思います。
————————————
▼1. Visual Studio Code を利用して C++ のコードを書いてみる
————————————
Visual Studio Code は Java 以外にも C や C++, C# など色々な言語を Windows, Linux, Mac 上で実行できる環境を提供しています。今回は以下のドキュメントを参考に C++ を試してみます。
Ref: Get Started with C++ on Linux in Visual Studio Code
————————————
▼2. 事前準備
————————————
2-1. Ubuntu 20.04.2 LTS の利用
Ref: https://releases.ubuntu.com/20.04/
2-2. Visual Studio Code のインストール
sudo snap install –classic code
Ref: https://code.visualstudio.com/docs/setup/linux
2-3. Microsoft C/C++ extension のインストール
Ref: https://code.visualstudio.com/docs/languages/cpp
2-4. GCC を含む GNU コンパイラーツールや GDB Debugger のインストール
Ref: https://code.visualstudio.com/docs/cpp/config-linux#_ensure-gcc-is-installed
sudo apt-get update
sudo apt-get install build-essential gdb2-5. フォルダおよびサブフォルダーを作成し、helloworld のフォルダを workspace とし、code . を実行すると、この workspace を利用した新たに VS code が開かれます。
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .————————————
▼3. C++ で Hello World を表示する
————————————
3-1. 上記 2 で helloworld フォルダを作成後、フォルダ配下に以下の C++ のファイル (helloworld.cpp) を作成し Hello World を含む文字列を表示します。
(例) helloworld.cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
} 3-2. C++のコードをコンピュータが読める機械語に翻訳するため、ビルド/コンパイルの方法を UI で指定します。指定後 tasks.json ファイルが作成されます。
VS Code のメニューにある Terminal -> Configure Default Build Task から C/C++:g++ build active file. /usr/bin/g++ を指定します。その後以下のファイルが生成されます。
//tasks.json の例
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}3-3. VS Code のメニューにある Terminal -> Run Build Task をクリックし、ビルド/コンパイルします。ビルドすると helloworld の実行ファイルが生成されます。
(出力の例)
Hello C++ World from VS Code and the C++ extension! 3-4. 最後に、デバッグとしてプログラムが実行したシステムコールやその引数および返り値を確認するために strace を実行してみました。
strace -t -o ./stracehelloworld.log ./helloworld(例) 標準出力
Hello C++ World from VS Code and the C++ extension! (例) stracehelloworld.log
15:00:26 execve("./helloworld", ["./helloworld"], 0x7fff4ecfd198 /* 54 vars */) = 0
15:00:26 brk(NULL) = 0x55bfeff62000
15:00:26 arch_prctl(0x3001 /* ARCH_??? */, 0x7ffec8d269f0) = -1 EINVAL (Invalid argument)
15:00:26 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
15:00:26 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
15:00:26 fstat(3, {st_mode=S_IFREG|0644, st_size=68977, ...}) = 0
15:00:26 mmap(NULL, 68977, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fb4042d2000
xxxx
xxxx
15:00:26 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x1), ...}) = 0
15:00:26 write(1, "Hello C++ World from VS Code and"..., 53Hello C++ World from VS Code and the C++ extension!
) = 53
15:00:26 exit_group(0) = ?
15:00:26 +++ exited with 0 +++ ————————————
▼4. 参考情報
————————————
Java – Visual Studio Code の利用 on Ubuntu No.24
C++ – Visual Studio Code の利用 on Ubuntu No.26
Python – Visual Studio Code の利用 on Ubuntu No.34
以上です。参考になりましたら幸いです。