The file() Command

Let's learn about the file() command in CMake.

This command provides all kinds of operations related to files:

  • Reading

  • Writing

  • Transferring

  • Locking

  • Archiving

It also provides modes to inspect the filesystem and operations on strings representing paths. Full details can be found in the online documentation.


The following modes are available:

  • file(READ <filename> <out> [OFFSET <o>] [LIMIT <max>] [HEX]) reads the file from <filename> to the <out> variable. The read optionally starts at offset <o> and follows the optional limit of <max> bytes. The HEX flag specifies that output should be converted to hexadecimal representation.

  • file(STRINGS <filename> <out>) reads strings from the file at <filename> to the <out> variable.

  • file(<algorithm> <filename> <out>) computes the <algorithm> hash from the file at <filename> and stores the result in the <out> variable. Available algorithms are the same as for the string() hashing function.

  • file(TIMESTAMP <filename> <out> [<format>]) generates a string representation of a timestamp of the file at <filename> and stores it in the <out> variable. Optionally accepts a <format> string.

  • file(GET_RUNTIME_DEPENDENCIES [...]) gets runtime dependencies for specified files. This is an advanced command to be used only in install(CODE) or install(SCRIPT) scenarios.

Get hands-on with 1200+ tech skills courses.