LoginSignup
0
1

More than 3 years have passed since last update.

VSCodeのIntelliSenseにpthread_rwlock_tを見つけてもらう方法

Posted at

はじめに

ここでは、VSCodeのIntelliSenseにpthread_rwlock_tを見つけてもらう方法を自分用にメモしておきます。

環境

  • OS: Ubuntu 18.04.02 LTS
  • VSCode 1.42.1
    • C/C++ for Visual Studio Code 0.26.3
  • gcc 7.4.0

起きた問題

VSCodeを使って、pthreadを使ったマルチスレッドプログラミングしていましたが、
キャッシュ関連処理でpthread_rwlock_tを使おうとすると、IntelliSenseに

識別子 "pthread_rwlock_t" が定義されていません

と怒られました。

しかし、pthread_rwlock_tを使う簡単なサンプルプログラムを作成してコンパイルしてみたところ、問題なくコンパイルでき実行もできました。

解決方法

https://github.com/microsoft/vscode-cpptools/issues/3547
このissueの内容で解決できました。ありがとうございます!

issueによると、ソースコードがあるフォルダ内の.vscode/c_cpp_properties.jsonを開いて、"defines"の中に"_GNU_SOURCE"を追加することで解決できるようです。

私の場合は以下のようになりました。

.vscode/c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_GNU_SOURCE"
            ],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "compilerArgs": [
                "-pthread"
            ]
        }
    ],
    "version": 4
}

以上により、pthread_rwlock_tに出ていたエラーがなくなりました!

原因

この問題が起こる原因は、issueを読む感じ

  • gccは標準でgcc extensionsという拡張が有効化されている。
    • pthread_rwlock_tを有効化させる定数がpreprocessor defineで定義される。
  • しかし、vscodeのIntelliSenseではこの拡張部分が無効化されている。
    • preprocessor defineで定数が定義されていないため、pthread_rwlock_tの定義をvscodeは見つけられない。

といったところのようです。そこで、C/C++プラグインの設定ファイルに、preprocessor defineに定義されている定数を追加してやることで問題を解決しているみたいです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1