2016-06-29 330 views
0

我有一個路徑,我想擺脫一個特定的路徑 開始路徑。這是我目前的路徑CMake的 - 使用get_filename_component

macro(MY_MACRO base) 

    foreach(ITEM ${ARGN}) 
     get_filename_component(ITEM_PATH ${ITEM} DIRECTORY) 
     get_filename_component(ITEM_EXT ${ITEM} EXT) 
     source_group("${ITEM_PATH}" FILES ${ITEM}) 
     MESSAGE ("${ITEM_PATH}") 
    endforeach() 

    endmacro() 

上述輸出這個

/Users/admin/main/project/module/pilot/pilot/src/proA 
/Users/admin/main/project/module/pilot/guide/src/proB 
得到一個路徑

我希望它只顯示從最後一級開始的路徑 它應該只顯示文件夾試點 之後的路徑,所以它應該顯示類似這樣的內容

pilot/src/proA 
guide/src/proB 

不完整路徑

+0

只是好奇,爲什麼? – Joel

回答

1

命令file(RELATIVE_PATH)計算相對路徑。用法很簡單:

file(RELATIVE_PATH 
    ITEM_PATH_REL # Output variable 
    "/Users/admin/main/project/module/pilot" # Base directory 
    ${ITEM_PATH} # Absolute path to the file 
) 
message("Relative path: ${ITEM_PATH_REL}") 
+0

當我嘗試這樣說它說文件RELATIVE_PATH必須通過文件的完整路徑: –

+0

是的,最後一個參數應該是全路徑。這確實是寫在這個例子的評論中。 – Tsyvarev