[CMake] TRY_COMPILE without linking...

Bill Hoffman bill.hoffman at kitware.com
Tue May 25 22:03:51 EDT 2010


On 5/25/2010 8:57 PM, Michael Hertling wrote:
> On 05/25/2010 06:13 PM, Theodore Papadopoulo wrote:
>> In porting a library (blitz) from autoconf to cmake, I have the
>> sub-project of testing C++ compiler features.
>> The autoconf way was to create some C++ files and test that they are
>> compiling (and just compiling not linking).
>>
>> TRY_COMPILE (in the variant that creates automatically the CMake
>> project) seems to oblige me to link the code.
>> Many of the tests (taken from autoconf) do not include a main(), some
>> the tests fail. Obviously, I can create a main,
>> but I wondered if there was an easy way to avoid the linking phase
>> (without having to create my own CMake project
>> for all those files).
>
> Use the CMAKE_FLAGS of TRY_COMPILE() to pass in a no-op for the linker:
>
> TRY_COMPILE(...
>      CMAKE_FLAGS "-DCMAKE_CXX_LINK_EXECUTABLE='echo not linking now...'"
> ...)


That will not work with Xcode and VS.  The best you can do with those 
generators is to create a static library instead of a linked executable.

-Bill


More information about the CMake mailing list