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?

【備忘録】CMakeで,フォルダをまたいだ依存関係を解決したい

Posted at

結論から言うと,何も特別なことをする必要はなかった.

詳しい状況

以下のようなフォルダ構成を考える.

.
├── lib1/
│   ├── include/
│   │   └── lib1.hpp
│   ├── src/
│   │   └── lib1.cpp
│   └── CMakeLists.txt
├── lib2/
│   ├── include/
│   │   └── lib2.hpp
│   ├── src/
│   │   └── lib2.cpp
│   └── CMakeLists.txt
├── main.cpp
└── CMakeLists.txt

ここで,lib2はlib1に依存しているとする.(このようなフォルダ構成,依存関係が健全なものかは知らない.)
このとき,それぞれのCMakeLists.txtはどのように設定すればいいだろうか.

解決策

こんな感じにやったらうまくいった.
正直よくわかっていないし,そのうち問題が発生するかもしれないが,今のところはこれで何とかなっている.

lib1/CMakeLists.txt
cmake_minimum_required(VERSION 4.0)
project(lib1 LANGUAGES CXX)

add_library(lib1 STATIC ./src/lib1.cpp)
target_compile_features(lib1 PRIVATE cxx_std_11)
target_include_directories(lib1 PUBLIC ./include)
lib2/CMakeLists.txt
cmake_minimum_required(VERSION 4.0)
project(lib2 LANGUAGES CXX)

add_library(lib2 STATIC ./src/lib2.cpp)
target_compile_features(lib2 PRIVATE cxx_std_11)
target_include_directories(lib2 PUBLIC ./include)
./CMakeLists.txt
cmake_minimum_required(VERSION 4.0)
project(main LANGUAGES CXX)
add_subdirectory(./lib1)
add_subdirectory(./lib2)
add_executable(main main.cpp)
target_link_libraries(lib2 lib1)
target_link_libraries(main lib2)
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?