Cmake - Getting a Path Using Get_Filename_Component

Cmake - Getting a Path Using Get_Filename_Component

I have a path and i would like to get the path starting from a specific path This is my current path

   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()

The above outputs this

/Users/admin/main/project/module/pilot/pilot/src/proA
/Users/admin/main/project/module/pilot/guide/src/proB

I would like it to just display the path starting from the last level it should only show the path after the folder pilot so it should show something like this

pilot/src/proA
guide/src/proB

Not the full path

1

1 Answer

Command file(RELATIVE_PATH) computes relative path. Usage is straightforward:

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}")
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.