[CMake] How to have a static/shared option in a Find script ?

Michael Hertling mhertling at online.de
Wed Feb 15 18:47:19 EST 2012


On 02/15/2012 03:48 PM, Barth wrote:
> Hello, 
> 
> I am trying to write a Find script for a library called DIM. It is something
> basic but I have a problem with caching. I have an option to force choosing
> the static library over the shared one : 
> 
> Then, I decide what is the name of the library to search depending on
> DIM_USE_STATIC and I find it with find_library : 
> 
> 
> The problem is that modifying DIM_USE_STATIC in ccmake doesn't work even
> though DIM_LIB_NAME is correct (ie. libdim.a). DIM_LIBRARY sticks to the
> previous value (the shared library). 
> I know that find_library will not run again if it has already found the
> library in the past, thus how should I do ? 
> 
> Thank you in advance for your help, 
> Barth

Probably, you just need to reset DIM_LIBRARY to an empty string in
ccmake each time you change DIM_USE_STATIC; see the FIND_LIBRARY()
documentation for more information: "If the library is found the
result is stored in the variable and the search will not be
repeated *unless the variable is cleared*."

However, a conceptually cleaner approach is to consider the shared and
the static version of a library as two components of a multi-component
package, and write the find module / configuration file accordingly:

(1) Use FIND_LIBRARY() to look for the shared and the static library
    and define DIM_SHARED_LIBRARY and DIM_STATIC_LIBRARY in the cache.
(2) Inspect DIM_FIND_COMPONENTS to see which flavor has been requested,
    defaulting to "shared" if no components have been requested at all.
(3) Warn or bail out if "shared" and "static" have both been requested
    unless they can be used together - rare but not impossible a priori.
(4) DIM_USE_STATIC decides if DIM_LIBRARIES receives DIM_STATIC_LIBRARY
    or DIM_SHARED_LIBRARY, and because DIM_LIBRARIES is not cached, it
    can be set anew each time FIND_PACKAGE(DIM ...) is called, so the
    issue you report on will go away.

IMO, most packages providing a library with shared and static versions
should be considered in this manner, as this would be a robust mean to
specifically select one or the other without the need to reset cache
entries or bother with CMAKE_FIND_LIBRARY_SUFFIXES or the like.

BTW, this approach would also account for the long-standing annoyance
how to have FIND_LIBRARY() differentiate between a static library and
an import library on Windows; each library type would simply have its
own FIND_LIBRARY() call, and this would make things much easier.

Regards,

Michael


More information about the CMake mailing list