I want to set compiler path (for example:icc) automatically in cmake, so my program can compile at any computer as long as it have installed icc, and we do not need to care about where the icc is installed.
At First, I using the follow command to set compiler. Everything is OK.
set(Intel_C_COMPILER "/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icc")
set(Intel_CXX_COMPILER "/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc")
set(CMAKE_C_COMPILER   ${Intel_C_COMPILER}  )
set(CMAKE_CXX_COMPILER ${Intel_CXX_COMPILER})
project(MyProject)
....
Then, I want to set compiler path automatically, I know that the follow command can find compiler path
which icc
So I write the follow command try to set compiler automatically by cmake.
execute_process(COMMAND which icc  OUTPUT_VARIABLE Intel_C_COMPILER)
execute_process(COMMAND which icpc OUTPUT_VARIABLE Intel_CXX_COMPILER)
message(Intel_CXX_COMPILER: ${Intel_C_COMPILER})
message(Intel_CXX_COMPILER: ${Intel_CXX_COMPILER})
set(CMAKE_C_COMPILER   ${Intel_C_COMPILER}  )
set(CMAKE_CXX_COMPILER ${Intel_CXX_COMPILER})
project(MyProject)
....
At these case, something strange happens, cmake shows that:
 Intel_CXX_COMPILER:/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
 CMake Error at CMakeLists.txt:27 (project):   The CMAKE_C_COMPILER:
    /opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icc
  is not a full path to an existing compiler tool.
  Tell CMake where to find the compiler by setting either the
environment   variable "CC" or the CMake cache entry CMAKE_C_COMPILER
to the full path to   the compiler, or to the compiler name if it is
in the PATH.
CMake Error at CMakeLists.txt:27 (project):   The CMAKE_CXX_COMPILER:
    /opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc
  is not a full path to an existing compiler tool.
  Tell CMake where to find the compiler by setting either the
environment   variable "CXX" or the CMake cache entry
CMAKE_CXX_COMPILER to the full path   to the compiler, or to the
compiler name if it is in the PATH.
 -- Configuring incomplete, errors occurred!
CMake said that the path is not a full path to an existing compiler, but as shown in the message, it is just where the compiler located!
I know there are other techniques that we can set compiler, for example export some environment variables to help cmake find the path, but I want to know why my method dose not work?
Is there any better way can handle this problem?
Thanks in advance.