[CMake] Using generator expression with add_library

Andrew Fuller afuller at teradici.com
Thu Nov 7 10:50:46 EST 2019


Another option is to use BUILD_SHARED_LIBS to control static vs. shared.  Set it to false on MSVC and true everywhere else, then omit the library type in the add_library calls that you wish to have controllable behaviour.  Those add_library calls will then consult BUILD_SHARED_LIBS to determine whether to generate a static or shared lib.
________________________________
From: CMake <cmake-bounces at cmake.org> on behalf of David Aldrich <david.aldrich.ntml at gmail.com>
Sent: November 7, 2019 3:52 AM
To: Petr Kmoch <petr.kmoch at gmail.com>
Cc: CMake MailingList <cmake at cmake.org>
Subject: Re: [CMake] Using generator expression with add_library

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<mailto: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<mailto: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<http://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/0d4f6af7/attachment-0001.html>


More information about the CMake mailing list