0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CMakeでGoogleTestを導入したときに入力補完が有効にならない時の対応

Posted at

これはなに?

タイトルの通り、CMakeでGoogleTestを導入した際に、NeoVimの入力補完が有効にならないことがあったので、備忘録として残しておきます。

環境

  • Windows WLS2
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.4 LTS"
  • エディタ: Neovim
    • 入力補完を表示するため、clangdを使用しています
    • 細かい設定はこちら

GoogleTestの導入

C++で開発をする際、GoogleTestを使用し、単体テストを書くことがあると思います。
まずは、導入方法を記載しますが、基本的には公式のサンプル通りに書けば、テストを実行することはできます。

cmake_minimum_required(VERSION 3.14)
project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
  hello_test
  hello_test.cc
)
target_link_libraries(
  hello_test
  GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(hello_test)

開発時にエラーが出る

上記のように、公式のCMakeのままでも、テスト自体を実行し結果を得ることはできるのですが、エディター上でライブラリが見つからないエラーとなり、入力補完が効かない状態となりました。
なので、まずCMakeに、

cmake_minimum_required(VERSION 3.14)
project(my_project)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

+ find_package(GTest REQUIRED)

add_executable(
  hello_test
  hello_test.cc
)
target_link_libraries(
  hello_test
  GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(hello_test)

を追加し、ライブラリを自動で見つけるようにします。
しかし、これでもエラーは解消されませんでした。
いろいろ調べた結果、stackoverflowの以下のやり取りを見つけました。

記事を読むと、以下の記述があったので、"libgtest-dev"をインストールすることで、エラーを解消することができました。

This happened to me when I install gtest but not gtest-devel. On Ubuntu, I'd guess you need something like sudo apt-get install libgtest-dev

おわりに

一番の原因は、ライブラリが足りていないことでした。
CMakeは奥が深い。

調べている中ではあまり情報が見つからなかったので、この記事が誰かの助けになれば幸いです。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?