CMAKE_CXX_COMPILER_ARCHITECTURE_ID is empty even when passed to cmake in command line with -D
I passed -DCMAKE_CXX_COMPILER_ARCHITECTURE_ID=x64 in command line to cmake:
cmake ../ -DCMAKE_CXX_COMPILER_ARCHITECTURE_ID=x64
and in CMakeLists.txt I have the following lines:
message(STATUS "CMAKE_CXX_COMPILER_ARCHITECTURE_ID: ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
end it prints empty variable. If run cmake for second time it prints correct value.
Then I changed my CMakeLists.txt to the following:
message(STATUS "CMAKE_CXX_COMPILER_ARCHITECTURE_ID: ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
set (CMAKE_CXX_COMPILER_ARCHITECTURE_ID ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})
message(STATUS "CMAKE_CXX_COMPILER_ARCHITECTURE_ID: ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
remove the build directory and run cmake ../ -DCMAKE_CXX_COMPILER_ARCHITECTURE_ID=x64 and then:
-- CMAKE_CXX_COMPILER_ARCHITECTURE_ID:
-- CMAKE_CXX_COMPILER_ARCHITECTURE_ID: x64
Also I added message in Modules/CMakeDetermineCXXCompiler.cmake
if(CMAKE_CXX_COMPILER_ARCHITECTURE_ID)
...
else()
message(STATUS "empty CMAKE_CXX_COMPILER_ARCHITECTURE_ID")
endif()
and it is printed.
I tested with: cmake version 3.10.2 (on Ubuntu 18.04) and cmake version 3.12.3
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(TestCxxArhId)
message(STATUS "CMAKE_CXX_COMPILER_ARCHITECTURE_ID: ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
####### workaround CMAKE_CXX_COMPILER_ARCHITECTURE_ID to be set by command line
set (CMAKE_CXX_COMPILER_ARCHITECTURE_ID ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})
message(STATUS "CMAKE_CXX_COMPILER_ARCHITECTURE_ID: ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
SET(srcfiles main.cpp CACHE INTERNAL "" FORCE) # main.cpp -> just an empty main() function
add_executable(demo ${srcfiles})
Here is a simple main.cpp
int main() {}
Edited by Brad King