Reputation: 18846
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?
Upvotes: 88
Views: 60586
Reputation: 41
Circumventing the warning by outputting the value of CMAKE_TOOLCHAIN_FILE
as Roland Sarrazin suggested is a clever solution. However, there is a possibility that the warning is legitimate since it is not possible to change the toolchain after the initial configuration. It would be ideal if we only got a warning if the user attempts to set a different CMAKE_TOOLCHAIN_FILE then used in a previous configuration. That can be done by creating a secret cache variable that holds the initial value.
if(DEFINED _initial_CMAKE_TOOLCHAIN_FILE
AND NOT _initial_CMAKE_TOOLCHAIN_FILE STREQUAL CMAKE_TOOLCHAIN_FILE)
message(WARNING "The CMAKE_TOOLCHAIN_FILE cannot be changed")
elseif(DEFINED CMAKE_TOOLCHAIN_FILE)
set(_initial_CMAKE_TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}" CACHE INTERNAL "")
endif()
Since the logic uses the value of CMAKE_TOOLCHAIN_FILE, the cmake warning is avoided and replaced by our more targeted warning.
Upvotes: 3
Reputation: 10971
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.
Upvotes: 82
Reputation: 1345
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}")
Upvotes: 20
Reputation: 517
Same happened here, as @js pointed out, this usually means you have build relicts of cmake from a past config.
Do a rm -rf CMakeCache.txt CMakeFiles/
and the message will be gone the first time you do the cmake -DCMAKE_TOOLCHAIN_FILE=foo.cmake .
. The second time, they will be there again and as @Guilaume answered that's ok.
P.S.:
I first did a git clean --force
but since those files are usually in .gitignore
, that does not reset the build.
Upvotes: 2
Reputation: 20897
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.
Upvotes: 6