Why am I warned that the "CMAKE_TOOLCHAIN_FILE" variable is not used by the project?

When I build some third-party code, I am seeing the following warning from CMake:

CMake Warning:
  Manually-specified variables were not used by the project:

    CMAKE_TOOLCHAIN_FILE

What is causing this warning? I checked the configuration files, but could not find anywhere where this variable is defined.

Should I be concerned about this warning? How can I fix it so that the warning goes away?


Solution 1:

This is the standard warning that CMake generates when you're giving it a command line option that it's not using. For example, passing -DFOO=bar to cmake when the CMakeLists.txt file doesn't actually use the FOO variable.

Now, this is a bit of a special case: CMAKE_TOOLCHAIN_FILE is used by CMake the first time it configures your build, but, since you can't change the toolchain for an already-configured build, this variable is ignored every other time; hence the warning.

As Answeror noted in a comment, you can safely ignore the warning. Brad King explained on the CMake mailing list on February 7, 2011:

I can only get this to happen by running CMake on a build tree that already exists. The variable *does* get used on the *first* run in a fresh tree. It does *not* get used later because you don't need to specify it to regenerate. CMake has already recorded a rule to load the file in CMakeFiles/CMakeSystem.cmake and does not support changing the toolchain of an existing build tree.

IOW, this is a legitimate instance of the warning.

If this warning really bothers you, you have a couple of options for suppressing it:

  • You can pass the --no-warn-unused-cli option when you run cmake.

    This is a bit of a blunt instrument, though, because it suppresses all warnings from unused variables specified on the command line. That may hide some legitimate warnings.

  • You can remove the temporary files generated by the first invocation of CMake before invoking it a second time. As mentioned in a comment by js., you need to delete the CMakeCache.txt file and the CMakeFiles folder. For example, by executing:

     rm -rf CMakeCache.txt CMakeFiles/
    

    But, as javs notes, this is a bit pointless. There's no reason to delete these generated files unless you are actually changing the toolchain. Although it does make the warning go away, it does so merely by forcing the files to be re-generated, which wastes time for minimal gain.

  • A third option (and perhaps the best) is given by Roland Sarrazin's answer. This involves simply using the variable by outputting it in a status message. This way, it is not unused, so the warning is not triggered. You may even find the message to be useful for debugging problems related to the toolchain configuration.

Solution 2:

Guillaume's answer has already explained the reason why you are getting this specific unused variable warning.

A simple and useful way to circumvent the warning—without removing the temporary files generated by the first invocation of CMake—is to use the variable in a status message. For example:

MESSAGE(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}")

Solution 3:

In case you get this message with CLion after you set the -DCMAKE_TOOLCHAIN_FILE=xxx option, you'll want to delete all the CMake build directories.

Do

Tools-> CMake -> Show Generated CMake Files in File Manager

then delete all build directories. Then do

Tools-> CMake -> Reload the CMake Project

Once you do this, you will still get the warning, but at least it will be observed the first time cmake is run.