2017-03-21 39 views
0

我剛進入cmake是因爲我開始研究一個更大的項目。我需要添加模塊測試。有幾個嵌入式設備,每個嵌入式設備都有自己運行的應用程序。用於更大項目的cmake用普通代碼

大多數這些應用程序共享代碼。 所以代碼被分成模塊。問題是有些模塊在大多數其他模塊中使用。這些模塊是常見的,通用網絡和日誌。

的設備的簡化計劃(APP)具有以下結構:

. 
    |-- CMakeLists.txt (for app) 
    |-- LICENSE 
    |-- app 
    | |-- inc 
    | | |-- appfile1.hpp 
    | | `-- appfile2.hpp 
    | `-- src 
    |  |-- appfile1.cpp 
    |  |-- appfile2.cpp 
    |  `-- main.cpp 
    |-- comp 
    | |-- comp1 
    | | |-- CMakeLists.txt (for test) 
    | | |-- comp 
    | | | |-- CMakeLists.txt (for lib) 
    | | | |-- intf 
    | | | | |-- comp1file.hpp 
    | | | | `-- comp1file.hpp 
    | | | `-- src 
    | | |  |-- comp1file.cpp 
    | | |  `-- comp1file.cpp 
    | | `-- test 
    | |  `-- src 
    | |   `-- comp1testfile.cpp 
    | |-- comp2 
    | | |-- CMakeLists.txt (for test) 
    | | |-- comp 
    | | | |-- CMakeLists.txt (for lib) 
    | | | |-- inc 
    | | | | `-- comp2file1.hpp 
    | | | |-- intf 
    | | | | |-- comp2file2.hpp 
    | | | | |-- comp2file3.hpp 
    | | | | |-- comp2file4.hpp 
    | | | `-- src 
    | | |  |-- comp2file1.cpp 
    | | |  |-- comp2file2.cpp 
    | | |  `-- comp2file3.cpp 
    | | `-- test 
    | |  |-- inc 
    | |  | `-- comp2testfile.hpp 
    | |  `-- src 
    | |   `-- comp2testfile.cpp 
    | |-- common 
    | | |-- CMakeLists.txt (for test) 
    | | `-- comp 
    | |  |-- inc 
    | |  | |-- commonfile1.hpp 
    | |  | `-- commonfile2.hpp 
    | |  `-- src 
    | |   `-- commonfile1.cpp 
    | |-- common-net 
    | | |-- CMakeLists.txt (for test) 
    | | |-- comp 
    | | | |-- inc 
    | | | | |-- netfile1.hpp 
    | | | | |-- netfile2.hpp 
    | | | | |-- netfile3.hpp 
    | | | | |-- netfile4.hpp 
    | | | | |-- netfile5.hpp 
    | | | `-- src 
    | | |  |-- netfile1.cpp 
    | | |  |-- netfile2.cpp 
    | | |  |-- netfile3.cpp 
    | | |  |-- netfile4.cpp 
    | | |  |-- netfile5.cpp 
    | | `-- test 
    | |  |-- inc 
    | |  | |-- nettestfile1.hpp 
    | |  `-- src 
    | |   |-- nettestfile1.cpp 
    | |   |-- nettestfile2.cpp 
    | |   |-- nettestfile3.cpp 
    | |-- comp3 
    | | |-- CMakeLists.txt (for test) 
    | | |-- comp 
    | | | |-- CMakeLists.txt (for lib) 
    | | | |-- intf 
    | | | | |-- comp3file1.hpp 
    | | | | `-- comp3file2.hpp 
    | | | `-- src 
    | | |  `-- comp3file1.cpp 
    | | `-- test 
    | |  `-- src 
    | |   `-- comp3testfile1.cpp 
    | |-- log 
    | | `-- comp 
    | |  |-- inc 
    | |  | |-- logfile1.hpp 
    | |  | |-- logfile2.hpp 
    | |  |-- intf 
    | |  | |-- logfile3.hpp 
    | |  | |-- logfile4.hpp 
    | |  `-- src 
    | |   |-- logfile1.cpp 
    | |   |-- logfile2.cpp 
    | |   |-- logfile3.cpp 
    | |   `-- logfile4.cpp 
    | |-- comp4 
    | | |-- CMakeLists.txt (for test) 
    | | |-- comp 
    | | | |-- CMakeLists.txt (for lib) 
    | | | |-- intf 
    | | | | |-- comp4file1.hpp 
    | | | | |-- comp4file2.hpp 
    | | | | |-- comp4file3.hpp 
    | | | `-- src 
    | | |  |-- comp4file1.cpp 
    | | |  |-- comp4file2.cpp 
    | | |  `-- comp4file3.cpp 
    | | `-- test 
    | |  |-- inc 
    | |  | |-- comp4testfile1.hpp 
    | |  | `-- comp4testfile2.hpp 
    | |  `-- src 
    | |   |-- comp4testfile1.cpp 
    | |   |-- comp4testfile2.cpp 
    | |   |-- comp4testfile3.cpp 
    | |   |-- comp4testfile4.cpp 
    | |   `-- comp4testfile5.cpp 
    |-- gcc-4.8.cmake 
    |-- gcc-4.9.cmake 
    `-- gcc-default.cmake 

我知道它有很多的它「補償」,但命名沒有達到我..

每個comp有2個CMakefiles: 最高級的是創建一個單元測試可執行文件來測試模塊。 comp中的一個是製作一個圖書館。以便它可以與應用CMakefile中的add_subdirectory一起使用。

問題是,很多模塊都依賴於通用的公共網和日誌。 目前這些部分不是作爲庫構建的。我猜是因爲如果在多個模塊中使用add_subdirectory會產生問題嗎?這是乾淨的嗎?

這會導致: 每個從屬模塊都包含comp CMakefile中的常見標題。 在頂層(應用程序CMakefile),常見的源被添加到可執行文件。 但是對於每個模塊測試,我還必須將常見源添加到測試可執行文件。

這對我來說似乎很陌生,我認爲出了問題。但我不知道如何幹淨地解決它?

看看項目樹和需要獨立測試每個模塊。 是在頂層添加源的方法還是沒有完成?

+0

Ty爲鏈接是一個非常有用的閱讀。 – rinn2883

回答

4

所以,如果我理解正確,你有兩個單獨的問題:必須爲每個可執行文件添加公共源文件,並且必須爲每個可執行文件包含通用標頭。

你真正應該做的是使每個通用代碼庫成爲一個CMake靜態庫目標。 CMake解決了項目中庫和可執行文件之間的依賴關係,這就是你所擁有的。只要你有共同的代碼添加子目錄(普通,常見的網,和日誌)使用該代碼你可以做這樣的事情

target_link_libraries(comp2 common common-net log) 
在每個比較文件夾

的事情面前,CMake的將處理依賴關係和鏈接。更重要的是,您可以將包含路徑(以及其他內容)附加到目標,並且它們將自動應用於鏈接到它的任何內容。

所以,例如日誌庫,你會使用

target_include_directories(log PUBLIC inc intf) 

然後,任何事情聯繫到日誌庫會自動comp/log/comp/inccomp/log/comp/intf文件夾添加到它的頭路。

最後,如果我理解正確,在需要可執行測試時您是add_subdirectory -ing comp4/CMakeLists.txt文件,並且在需要構建用於包含到應用程序中的庫時使用comp4/comp/CMakeLists.txt文件。更好的方法是將測試鏈接到庫。

所以,總而言之,你的項目結構應該看起來有點像這樣:

頂級的CMakeLists.txt

add_subdirectory(comp) 
add_subdirectory(app) 

補償/的CMakeLists.txt

add_subdirectory(common) 
add_subdirectory(common-net) 
add_subdirectory(log) 
add_subdirectory(comp1) 
<and so on for the other comps> 

COMP/common-net/CMakeLists.txt

add_subdirectory(comp) 
add_executable(test-common-net <source for test>) 
target_link_libraries(test-common-net common-net) 

COMP /公共網/ COMP /的CMakeLists.txt

add_library(common-net STATIC <source for common-net>) 
target_include_directories(common-net PUBLIC inc) 

# assuming common-net depends on common 
target_link libraries(common-net common) 

COMP/COMP1 /的CMakeLists.txt

add_subdirectory(comp) 
add_executable(test-comp1 <source for test>) 
target_link_libraries(test-comp1 comp1) 

COMP/COMP1/COMP /的CMakeLists.txt

add_library(comp1 STATIC <source for common-net>) 
target_include_directories(comp1 PUBLIC inc) 

target_link libraries(comp1 common common-net log) 

你現在可能已經明白了。您只需創建一個相互依賴的庫鏈。

+0

ty我用你的指針重拍了一些東西。我認爲它更清潔。希望球隊會看到這種方式更好。 – rinn2883