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