0

In my project CMake, I'm using file(Globe ...):

file(GLOB SOURCES
   srcDir/srcA.cpp
   srcDir/srcB.cpp
   srcDir/srcC.cpp
   srcDir/srcD.cpp
 )

I read that file(GLOB ...) can be evil: cmake-file-glob-evil, glob pros / cons.

I can understand why using file(GLOB ...) with wildcard is not recommended:

file(GLOB SOURCES
   srcDir/*
 )

But I dont understand the difference between file(GLOB ...) to set(...) if I'm using it with specific files:

file(GLOB SOURCES
   srcDir/srcA.cpp
   srcDir/srcB.cpp
   srcDir/srcC.cpp
   srcDir/srcD.cpp
 )

VS

set(SOURCES
   srcDir/srcA.cpp
   srcDir/srcB.cpp
   srcDir/srcC.cpp
   srcDir/srcD.cpp
 )

############# Edit ##################

Side question: What's the "price" of using file(GLOB) instead of set()? Does it effect compilation time?

lior.i
  • 573
  • 1
  • 7
  • 20
  • 2
    `file(GLOB)` **searches** files by given glob(s) and assign to the variable only paths which has been found. `set` **unconditionally assigns** the given value to the variable. – Tsyvarev Jul 05 '21 at 12:29
  • Thanks @Tsyvarev, So is there down side of using ```file(glob)``` vs ```set()``` in this way? – lior.i Jul 05 '21 at 12:31
  • If you want to assign then use `set`, if you want to search then use `fild(glob)`. If depends what do you want. – Tsyvarev Jul 05 '21 at 12:55
  • 1
    If you `file` and there's no such file, you'll probably get linking errors (because you had headers and code compiled just fine, but implementations of functions are not found because missing source files were not added to compilation, thus missing links). If you `set` and there's no such file, you'll either get CMake configuration error or compilation error (because CMake will try to pass a nonexistent path to the compiler) – Alexey S. Larionov Jul 05 '21 at 13:08
  • Thanks, @Tsyvarev and @AlexeyLarionov from your answers it looks like ```set``` is more suitable for my project. (I think that you can write your comments as an answer) – lior.i Jul 05 '21 at 13:29
  • Side question: What's the "price" of using ```file(GLOB)``` instead of ```set()```? Does it effect compilation time? – lior.i Jul 05 '21 at 13:31
  • 2
    In a small-to-medium project the difference is hardly ever noticeable, but `set` is obviously as cheap as nothing, while `file(GLOB)` might take some time if the folder is very deep and contains hundreds of files (which is rare, unless it's some giant repository like Boost). – Alexey S. Larionov Jul 05 '21 at 14:06
  • Tsyvarev and @AlexeyLarionov, you had some great comments, could you please write them as an answer to my question? – lior.i Jul 13 '21 at 13:22

1 Answers1

1

If you do file and there's no such file, then you'll probably get linking errors (because since you had headers, your code compiled just fine, but implementations of functions will not be found because of missing source files that were not added to compilation, thus missing links).

If you do set and there's no such file, you'll either get CMake configuration error or compilation error (because CMake will try to pass a nonexistent path to the compiler).

set however is a very fast operation (basically set local variable to some constant), while file actually requires to deeply traverse the whole directory on File System and to match some pattern to each file on the way. It may be slow if you GLOB over a very big/deep folder.

Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37