[CMake] Using generator expression with add_library
David Aldrich
david.aldrich.ntml at gmail.com
Thu Nov 7 06:52:49 EST 2019
Thank you. So I guess I can make it as simple as:
if(MSVC)
add_library(${_star_lib_name} STATIC "")
else()
add_library(${_star_lib_name} SHARED "")
endif()
I just wondered if there was a more elegant way.
On Thu, Nov 7, 2019 at 11:45 AM Petr Kmoch <petr.kmoch at gmail.com> wrote:
> Hi.
>
> The argument STATIC or SHARED is processed at CMake configure time (that
> is, when CMake is executing the add_library() command). However, generator
> expressions are only evaluated at generate time, which comes only after all
> CMake code is processed.
>
> Fortunately for you, compiler ID is something that is already known at
> configure time, meaning you don't need to use a genex to read it. You can
> just do this:
>
> if(MSVC)
> set(LibType STATIC)
> else()
> set(LibType SHARED)
> endif()
> add_library(
> ${_star_lib_name}
> ${LibType}
> ...
> )
>
> (Feel free to modify the if(), use CMAKE_<LANG>_COMPILER_ID (
> https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html)
> etc. as necessary).
>
> Petr
>
> On Thu, 7 Nov 2019 at 12:28, David Aldrich <david.aldrich.ntml at gmail.com>
> wrote:
>
>> I want to build a shared library for Linux and a static library for
>> Windows. So I have tried:
>>
>> set (_star_lib_name "StdStars")
>>
>> add_library(${_star_lib_name}
>> $<$<CXX_COMPILER_ID:GNU>:SHARED>
>> $<$<CXX_COMPILER_ID:MSVC>:STATIC>
>> ""
>> )
>>
>> but that gives me error:
>>
>> CMake Error at C:/SVNProj/zodiac/branches/TRY_TML_CMake_3Oct2019/StarLibs/StdStars/CMakeLists.txt:7 (add_library):
>> Cannot find source file:
>>
>> STATIC
>>
>> Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
>> .hpp .hxx .in .txx
>>
>> What is the correct way of doing this please?
>> --
>>
>> Powered by www.kitware.com
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Kitware offers various services to support the CMake community. For more
>> information on each offering, please visit:
>>
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Follow this link to subscribe/unsubscribe:
>> https://cmake.org/mailman/listinfo/cmake
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191107/c2068ac7/attachment.html>
More information about the CMake
mailing list