<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
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.</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> CMake <cmake-bounces@cmake.org> on behalf of David Aldrich <david.aldrich.ntml@gmail.com><br>
<b>Sent:</b> November 7, 2019 3:52 AM<br>
<b>To:</b> Petr Kmoch <petr.kmoch@gmail.com><br>
<b>Cc:</b> CMake MailingList <cmake@cmake.org><br>
<b>Subject:</b> Re: [CMake] Using generator expression with add_library</font>
<div> </div>
</div>
<div>
<div dir="ltr">Thank you. So I guess I can make it as simple as:
<div><br>
</div>
<div>if(MSVC)<br>
    add_library(${_star_lib_name} STATIC "")<br>
else() <br>
    add_library(${_star_lib_name} SHARED "")<br>
endif()<br>
</div>
<div><br>
</div>
<div>I just wondered if there was a more elegant way.</div>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Thu, Nov 7, 2019 at 11:45 AM Petr Kmoch <<a href="mailto:petr.kmoch@gmail.com">petr.kmoch@gmail.com</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div dir="ltr">
<div>Hi.</div>
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>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:</div>
<div><br>
</div>
<div>if(MSVC)</div>
<div>  set(LibType STATIC)</div>
<div>else()</div>
<div>  set(LibType SHARED)</div>
<div>endif()</div>
<div>add_library(</div>
<div>  ${_star_lib_name}</div>
<div>  ${LibType}</div>
<div>  ...</div>
<div>)</div>
<div><br>
</div>
<div>(Feel free to modify the if(), use CMAKE_<LANG>_COMPILER_ID (<a href="https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html" target="_blank">https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html</a>) etc. as necessary).</div>
<div><br>
</div>
<div>Petr<br>
</div>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Thu, 7 Nov 2019 at 12:28, David Aldrich <<a href="mailto:david.aldrich.ntml@gmail.com" target="_blank">david.aldrich.ntml@gmail.com</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div dir="ltr">
<p style="margin-top:0px; font-family:Helvetica,Arial,sans-serif; font-size:15px">
I want to build a shared library for Linux and a static library for Windows. So I have tried:</p>
<pre style="overflow:auto; font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace; font-size:15px"><code style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace; font-size:1em; overflow:auto; background:none 0% 0% repeat scroll rgb(249,249,249); display:block; padding:0.5em; max-height:500px"> set (_star_lib_name "StdStars")
 
 add_library(${_star_lib_name} 
             $<$<CXX_COMPILER_ID:GNU>:SHARED>
             $<$<CXX_COMPILER_ID:MSVC>:STATIC> 
             ""
 )
</code></pre>
<p style="font-family:Helvetica,Arial,sans-serif; font-size:15px">but that gives me error:</p>
<pre style="overflow:auto; font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace; font-size:15px"><code style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace; font-size:1em; overflow:auto; background:none 0% 0% repeat scroll rgb(249,249,249); display:block; padding:0.5em; max-height:500px">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
</code></pre>
<p style="font-family:Helvetica,Arial,sans-serif; font-size:15px">What is the correct way of doing this please?</p>
</div>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">
http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">
http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">
http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">
http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">
http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote>
</div>
</blockquote>
</div>
</div>
</body>
</html>