Domenico Paolicelli
Domenico Paolicelli

Reputation: 97

CMake glob list of files without directory path

If you glob a list of files with file(GLOB..) in CMake, the files are all listed with their full path attached.

How can I glob a group of files in CMake saving only the name of the files?

I read the official manual and here I found this:

 file(GLOB <variable>
 [LIST_DIRECTORIES true|false] [RELATIVE <path>]
 [<globbing-expressions>...])

and

 By default GLOB lists directories - directories are omitted in result 
 if LIST_DIRECTORIES is set to false.

I tried these solutions, but none of them works:

file(GLOB_RECURSE src_files LIST_DIRECTORIES false ${SRC})

file(GLOB_RECURSE src_files LIST_DIRECTORIES=false ${SRC})

file(GLOB_RECURSE src_files LIST_DIRECTORIES(false) ${SRC})

set(LIST_DIRECTORIES false)
file(GLOB_RECURSE src_files ${SRC})

My output is always the list of the files with full path attached. I know I could use RELATIVE to set the path accordingly, but I would prefer to avoid it if I can.

Upvotes: 1

Views: 5589

Answers (2)

lol4-
lol4-

Reputation: 1

Try

file(GLOB_RECURSE LIST_DIRECTORIES false src_files ${SRC})

Documentation is erroneous, LIST_DIRECTORIES comes before variable

Upvotes: -3

Michał Walenciak
Michał Walenciak

Reputation: 4369

LIST_DIRECTORIES is used to include/exclude directories in your results in situation when given location ${SRC} contains both files and directories.

If you want to get rid of path, use foreach along with get_filename_component on results from file(GLOB_RECURSE...)

Upvotes: 5

Related Questions