COMPILE_LANGUAGE requires the language to be loaded
Currently, the following MWE CMakeLists.txt
will fail:
cmake_minimum_required(VERSION 3.4)
project(CCompileCheck LANGUAGES CXX)
file(WRITE simple.cxx [=[
#include <stdio.h>
int main() {
printf("Language: %s\n", LANG);
return 0;
}
]=])
add_executable(simple simple.cxx)
target_compile_options(simple PUBLIC
$<$<COMPILE_LANGUAGE:CXX>:-DLANG=\"CXX\">
$<$<COMPILE_LANGUAGE:C>:-DLANG=\"C\">)
You have to load C
and CXX
as the languages to get this to run. However, if this was, for example, setting up an INTERFACE
target in a helper cmake file, and you wanted to provide flags for both C
and CXX
, you'd have to check CMAKE_CXX_COMPILER_LOADED
and CMAKE_C_COMPILER_LOADED
and only append the flags if those were true.
IMO, a much more natural solution would be to also evaluate COMPILE_LANGUAGE:X
to false if X
is not loaded.
Edited by Henry Schreiner