[CMake] test linker flags?

Michael Hertling mhertling at online.de
Sun Jul 24 20:22:23 EDT 2011


On 07/15/2011 03:15 AM, Clifford Yapp wrote:
> Is there a way to test flags supplied to the linker
> (CMAKE_SHARED_LINKER_FLAGS) in the same way we can test compiler flags
> with CHECK_C_COMPILER_FLAG?  I'd like to check if -Wl,--no-undefined
> works or not before using it.
> 
> Cheers,
> CY

You might set CMAKE_REQUIRED_FLAGS to the linker flags you want to test
and subsequently invoke CHECK_C_COMPILER_FLAG() with an empty argument:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(LINKERFLAGS C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
INCLUDE(CheckCCompilerFlag)
SET(CMAKE_REQUIRED_FLAGS "-Wl,--no-undefined")
CHECK_C_COMPILER_FLAG("" RESULT1)
IF(RESULT1)
    MESSAGE("Supported: -Wl,--no-undefined")
ELSE()
    MESSAGE("Not supported: -Wl,--no-undefined")
ENDIF()
SET(CMAKE_REQUIRED_FLAGS "-Wl,--nonono-undefined")
CHECK_C_COMPILER_FLAG("" RESULT2)
IF(RESULT2)
    MESSAGE("Supported: -Wl,--nonono-undefined")
ELSE()
    MESSAGE("Not supported: -Wl,--nonono-undefined")
ENDIF()

AFAICS, the reason CHECK_C_COMPILER_FLAG() doesn't work for flags
passed to the linker is that its first argument is appended to the
CMAKE_REQUIRED_DEFINITIONS variable before CHECK_C_SOURCE_COMPILES()
is called which in turn passes the CMAKE_REQUIRED_DEFINITIONS to the
COMPILE_DEFINITIONS argument of TRY_COMPILE(). So, the flag is just
used for the compilation, and flags like "-Wl,..." are ignored, i.e.
they pass always. However, the CMAKE_REQUIRED_FLAGS are appended to
TRY_COMPILE()'s CMAKE_FLAGS as "-DCOMPILE_DEFINITIONS:STRING=..."
and apparently make it to the linker command as can be seen in
CMakeFiles/CMakeTmp/CMakeFiles/cmTryCompileExec.dir/link.txt.

Perhaps, one should reconsider this behavior and provide an improved
possibility to check flags being meaningful for the linker also/only.

Regards,

Michael


More information about the CMake mailing list