※当サイトの記事には、広告・プロモーションが含まれます。

Visual Studio Codeの拡張機能Code RuunerでC言語のpthreadを動くようにしたかったが...

gigazine.net

⇧ 日本は統計的にも着実に貧しい国になっていると...

夢も希望もないですな...

Visual Studio Code拡張機能Code RuunerでC言語のpthreadを動くようにしたかったが...

驚くほど情報が少ないんだけど、

stackoverflow.com

⇧ 上記サイト様によりますと、tasks.jsonなるファイルに設定を追加すれば良いらしいと。

tasks.jsonはと言うと、

code.visualstudio.com

⇧ .vscodeフォルダを作成しておく必要があるそうな。

で、.vscodeはプロジェクトのルートディレクトリ直下に配置した方が良い。

前に、

ts0818.hatenablog.com

⇧ Dockerコンテナとホストのディレクトリをマウントしていたのだけど、コンテナ側のディレクトリのマウントがプロジェクトのルートディレクトリになっていなかったという...

悲報...

teratail.com

⇧ マウントのパスは途中で変更できないらしい...コンテナの初回起動時のみしかマウントのパスを設定できないという鬼畜仕様...

コンテナを作り直すしかないらしい...

実際の開発現場とかだと、DockerコンテナからDockerイメージファイルを作成することで、Dockerコンテナの情報を保持する必要がありますが、今回は、自宅学習用で、且つ、引き継ぎたい情報も無いので、Dockerイメージファイルを作らずに、新たにDockerコンテナを作成・起動する際のマウントで、パスを変更します。

.vscodeフォルダを作成します。

Visual Studio Codeで、稼働しているDockerコンテナに接続すると、.vscodeフォルダが作成できていることが確認できます。

では、tasks.jsonファイルを作成。

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build active file",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g",
//                "${file}",
                "${workspaceFolder}/app/src/main/**/**/*.c",
                "-o",
//                "${fileDirname}/${fileBasenameNoExtension}",
                "${workspaceFolder}/build/output",
                "-pthread"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Run Code",
//            "command": "${fileDirname}/${fileBasenameNoExtension}",
            "command": "${workspaceFolder}/build/output",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": ["build active file"]
        }
//        ,{
//            "label": "build and debug active file",
//            "type": "cppdbg",
//            "request": "launch",
//            "program": "${fileDirname}/${fileBasenameNoExtension}",
//            "args": [],
//            "stopAtEntry": false,
//            "cwd": "${workspaceFolder}",
//            "environment": [],
//            "externalConsole": false,
//            "MIMode": "gdb",
//            "setupCommands": [
//                {
//                    "description": "Enable pretty-printing for gdb",
//                    "text": "-enable-pretty-printing",
//                    "ignoreFailures": true
//                }
//            ],
//            "preLaunchTask": "build active file",
//            "miDebuggerPath": "/usr/bin/gdb"
//        }
    ]
}

⇧ 今回、gdbをインストールしていないので、デバッグの設定はコメントアウト

で、pthreadを利用するソースコードを用意。

■/home/project-c/app/src/main/pthread/sample/sampleThread.c

#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

void *threadFunc(void *arg) {
    int i;

    for(i = 0; i < 3; i++) {
        printf("I'm threadFunc: %d\n", i);
        sleep(1);
    }
    return NULL;
}

int main(void) {
    pthread_t thread;
    int i;

    if(pthread_create(&thread, NULL, threadFunc, NULL) != 0) {
        printf("Error: Failed to create new thread.\n");
        exit(1);
    }

    for(i = 0; i < 5; i++) {
        printf("I'm main: %d\n", i);
        sleep(1);
    }

    return 0;
}
    

で、拡張機能「Code Runner」で実行すると、

⇧ tasks.jsonの設定が無視されてる気がするんだが...

悲報...

github.com

⇧ どうやら、拡張機能「Code Runner」が、tasks.jsonの設定を読み込めないらしい...

いまのところ、

stackoverflow.com

⇧ .vscode/settings.jsonの設定で、拡張機能「Code Runner」の設定を上書きするしか無いらしい...

拡張機能「Code Runner」を使わず、タスクとして実行することはできるっぽいが...

何か、いろいろ微妙...

Visual Studio Codeいろいろ破綻してますな...

何か、pthreadオプションを指定しなくてもコンパイル・実行できていたのは、

GCCのバージョンによるとは思うのだけど、デフォルトの設定で「--enable-threads」になっていて、pthreadが有効になっているからということなんだろうか?

ネットの情報を見る限りだと、このあたりに触れてる情報が見当たらないんよね...

そもそも論として、

blog.inductor.me

⇧ Alpine LinuxをDockerイメージのベースとして使うのはメリットが無くなってきてるとのこと。

兎にも角にも、開発環境構築に関して、正確な情報が欲しいところですな...

毎度モヤモヤ感が半端ない...

今回はこのへんで。