2016-05-23 39 views
3

私は自分のC++プロジェクトの何が問題になるのか理解しようとしています。基本的に、プロジェクト1は正常に動作し、すべてが素晴らしいです。プロジェクトのメインのヘッダファイルでは、私はCMake - 依存関係を整理する方法

#include "spdlog/spdlog.h" 

を持っていると私はinclude_directories(spdlog/include)を持っているプロジェクト1のための私CMakeの中にさらにプロジェクト1のサブプロジェクトとしてspdlogてきました。さて、私はプロジェクト1をビルドしており、それはサブプロジェクトとして持っています。しかし、私がspdlogを含めると、それは私を許さず、完全に../project1/spdlog/include/spdlog.hを作りたいと思っています。この依存関係を整理し、ヘッダーを含める正しい方法は何ですか?

+0

これは次いで '../ Project1の/ spdlog /含む/ spdlog/spdlog.h'である必要はありませんか?とにかく 'include_directories()'のスコープは難しいかもしれません。ですから 'spdlog'サブディレクトリに独自の' CMakeLists.txt'ファイルがある場合、 'spdlog'に' target_include_directories(spdlog PUBLIC "include") 'という独自のインクルードディレクトリを伝播させることでこれを修正することができます。 – Florian

+0

私はサードパーティの依存関係としてspdlogを保持します。次に、ヘッダーとライブラリを探してみてください。 – usr1234567

+0

@ usr1234567これを行う方法をもっと詳しく説明できますか?これは実際にはヘッダのみのライブラリです。申し訳ありませんが、もしそれがばかげた質問であれば、私はC++とCMakeには新しく、正しい方法でやろうとしています。 –

答えて

0

最新のcmakeリソースを探してください。現代のcmakeスタイルでは、spdlogのImportedターゲットを作成し、他の場所で使用することができます。

external/CMakeLists.txtあなたがあなたのソリューション構成(例)プロジェクトに

project(mysolution) 
add_subdirectory(external) 
add_subdirectory(project1) 
add_subdirectory(project2) 

を書くCMakeLists.txtルートに

## add the imported library which is 
## an INTERFACE (ie header only) 
## IMPORTED (ie third party - no compilation necessary) 
## GLOBAL (you can reference it from everywhere 
##   even if the target is not in the same scope 
##   which is the case for project1 and project2 because 
##   the spdlog target is not in their parent folder) 
add_library(spdlog INTERFACE IMPORTED GLOBAL) 
## set the include directory which is to be associated with this target 
## and which will be inherited by all targets which use 
## target_link_libraries(<target> spdlog) 
target_include_directories(spdlog INTERFACE spdlog/include) 

を書くには、以下の構造

external/spdlog 
external/CMakeLists.txt 
project1/CMakeLists.txt 
project2/CMakeLists.txt 
CMakeLists.txt 

とさせて頂きますCMakeLists.txtあなたは今できる

[0] https://cmake.org/cmake/help/v3.9/command/add_library.html

:あなたはexternal

Project1の中で作成したターゲット/ CMakeLists.txt

add_library(project1 ${Sources}) 
## this will set the include directories configured in the spdlog target (library) 
## and also ensure that targets depending on target1 will have access to spdlog 
## (if PUBLIC were changed to PRIVATE then you could only use spdlog in 
## project1 and dependeing targets like project2 would not 
## inherit the include directories from spdlog it wouldn't 
target_link_libraries(project1 PUBLIC spdlog) 

のProject2/CMakeLists.txt

add_library(project2 ${Sources}) 
## this will link project1 which in return also links spdlog 
## so that now project2 also has the include directories (because it was inherited) 
## also targets depending on project2 will inherit everything from project1 
## and spdlog because of the PUBLIC option 
target_link_libraries(project2 PUBLIC project1) 

ソースを使用[1] https://cmake.org/cmake/help/v3.9/command/target_link_libraries.html?highlight=target_link_libraries

[2] http://thetoeb.de/2016/08/30/modern-cmake-presentation/(スライド20から始まるプレゼンテーションを参照)

関連する問題