1 # - Check whether the C linker supports a given flag.
2 # CHECK_C_LINKER_FLAG(FLAG VARIABLE)
4 # FLAG - the compiler flag
5 # VARIABLE - variable to store the result
7 # This actually calls the check_c_source_compiles macro.
8 # See help for CheckCSourceCompiles for a listing of variables
9 # that can modify the build.
11 # Copyright (c) 2010, Joerg Mayer (see AUTHORS file)
13 # Redistribution and use is allowed according to the terms of the BSD license.
15 INCLUDE(CheckCSourceRuns)
17 MACRO (CHECK_C_LINKER_FLAG _FLAG _RESULT)
21 # See CMake bug 0015934:
23 # https://cmake.org/Bug/view.php?id=15934
25 # So we add the flags to CMAKE_REQUIRED_LIBRARIES, to sneak it into
28 # This may or may not work with versions earlier than 2.8.11, although
29 # 2.8.10's Xcode generator doesn't appear to work at all - it fails
30 # with an internal CMake error.
32 # With 3.2 and later, we could also set policy CMP0056 to NEW and
33 # set CMAKE_EXE_LINKER_FLAGS.
35 set(save_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
36 set(CMAKE_REQUIRED_LIBRARIES "${_FLAG}")
37 if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
39 # This means the linker is presumably the Microsoft linker;
40 # we need to pass /WX in order to have the linker fail,
41 # rather than just complaining and driving on, if it's
42 # passed a flag it doesn't handle.
44 set(CMAKE_REQUIRED_LIBRARIES "/WX ${CMAKE_REQUIRED_LIBRARIES}")
45 elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
47 # We'll be running the linker through the compiler driver, so
48 # we may need to pass -Werror=unused-command-line-argument to have it
49 # fail, rather than just complaining and driving on, if it's
50 # passed a flag it doesn't handle.
51 set(CMAKE_REQUIRED_LIBRARIES
52 "-Werror=unused-command-line-argument ${CMAKE_REQUIRED_LIBRARIES}")
54 check_c_source_compiles("int main(void) { return 0;}" ${_RESULT})
55 set(CMAKE_REQUIRED_LIBRARIES "${save_CMAKE_REQUIRED_LIBRARIES}")
56 ENDMACRO (CHECK_C_LINKER_FLAG)