[CMake] creating a shared and static library of the same name

Brad King brad.king at kitware.com
Tue May 16 14:23:48 EDT 2006


Michael Biebl wrote:
> On 5/15/06, Xavier Delannoy <xavier.delannoy at netasq.com> wrote:
> 
>> it's possible with CMake greater to 2.3.4. I used two diferent 
>> destination directories for the static and shared libs.
>>
> 
> Thanks Xavier for this detailed (and lengthy) instructions. But they
> are way too complicated and cumbersome imho.
> Under Unix/Linux it's not that uncommon that you want to create both,
> a shared and dynamic library (of the same name of course). Why not add
> a new keyword to add_library like "BOTH" so it automatically creates
> both types. On platforms like windows, where there are potential
> problems as Brad outlined, you could output a warning or create a
> workaround like two different output folders automatically.

The interface to enable it is not an issue.  Creation of two targets 
with one list of sources could be packaged up in a macro.  The problem 
is supporting Windows where libraries almost never use the same name for 
static and shared versions.  Somehow you have to always use two directories.

For projects that don't care about windows using two libraries with the 
same name in the same directory is not a problem if one is shared and 
one is static.  Currently the only thing preventing this is the code 
that removes all possible names (shared or static) for a target before 
linking it.  This causes the targets in the same directory to clobber 
each other.  We can add a target property to disable this.  Then your 
code could look like this:

ADD_LIBRARY(foo-shared SHARED foo1.cxx)
SET_TARGET_PROPERTIES(foo-shared PROPERTIES
   OUTPUT_NAME foo CLEAN_DIRECT_OUTPUT 1)
ADD_LIBRARY(foo-static STATIC foo1.cxx)
SET_TARGET_PROPERTIES(foo-static PROPERTIES
   OUTPUT_NAME foo CLEAN_DIRECT_OUTPUT 1)

and could easily be package in a macro.  Note that the 
CLEAN_DIRECT_OUTPUT property does not yet exist and I made it up for 
this example.  It tells CMake to not remove the static copy of a library 
when linking the shared one and vice versa (currently this is done to 
avoid problems when a project switches between shared and static 
builds).  If you have a better name in mind please suggest it.

Comments?
-Brad


More information about the CMake mailing list