Search code examples
cmakeglob

What's the difference between CMake set() and CMake file(Globe)?


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?


Solution

  • 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.