引っかかったところの個人的メモです。
前提
以下のようなフォルダ構成のアプリケーションをCMakeでビルドする
- app
- component1
- include
- component1.h
- src
- component1.cpp
- CMakeList.txt
- include
- component2
- include
- component2.h
- src
- component2.cpp
- CMakeList.txt - main.cpp
- CMakeList.txt
- component1
やりたいこと
トップのCMakeLists.txtにてadd_subdirectoryをコールし、component1, component2をリンクする。
※component1はcomponent2に依存する
実装
CMakeList.txt(概要のみ)
add_subdirectory(component2)
add_subdirectory(component1 ${CMAKE_CURRENT_SOURCE_DIR}/component2)
add_executable(main main.cpp)
target_link_library(main component1 component2)
解説
subdirectory側では、基本的に上位階層を参照することができません。
そこで、2行目add_subdirectoryの第2パラメータ。これでcomponent1にcomponent2のパスを渡すことができるようになります。