[CMake] Static linking and find_library

Brad King brad.king at kitware.com
Wed May 13 16:09:22 EDT 2009


S. Levent Yilmaz wrote:
> How can one enforce static linkage on a per platform basis (not the whole project)? 
> In other words, what setting can we insert into a toolchain file 
> <http://www.vtk.org/Wiki/CMake_Cross_Compiling> to make this possible?
> 
> Let me identify a problem here with find_library(). Here is a repeatable 
> scenario; take the following 3 line CMakeLists.txt: 
> 
> project( foo CXX )
> cmake_minimum_required(VERSION 2.6)
> find_library( FOO_LIBRARY foo ENV LD_LIBRARY_PATH )
                                 ^^^^^^^^^^^^^^^^^^^
This is almost certainly a bad idea.  The find_library command already
provides lots of customization features.  See its documentation.
Also, if you're trying to build static libs then why search in dynamic
loader paths for libraries?

What happens in your cases when it is not done?

>    2. cmake -DCMAKE_SYSTEM_NAME=Generic ..
>    3. cmake -DCMAKE_SYSTEM_NAME=Catamount ..

I don't think direct definition of CMAKE_SYSTEM_NAME is supported.
It's supposed to be detected and set by the platform files.  Only
through a toolchain file should it be customized.  (Alex?)

> line (and the only line) in share/cmake-2.x/Modules/Platform/Generic.cmake:
> 
> SET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)

It's mostly for reference by CMakeLists.txt code in the project.

> and it seems to have no effect. [..]Platform/Catamount.cmake  also has 
> this line along with bunch of other stuff, and it successfully "forces" 
> find_library() to get to the static library. In Case(4) the custom 
> toolchain file sysbar.cmake is nothing but  set(CMAKE_SYSTEM_NAME 
> Generic) followed by the rest of the stuff in Catamount.cmake. Case(4), 
> quite confusingly, finds libfoo.so!!  

Your file sets CMAKE_FIND_LIBRARY_SUFFIXES but CMake loads the module
CMakeGenericSystem.cmake *after* the toolchain file and changes it back
to putting .so in the list.  The toolchain file is just supposed to
specify some very basic information which triggers loading of platform-
specific configuration.

The documentation you reference at

   http://www.cmake.org/Wiki/CMake_Cross_Compiling

says that a ${CMAKE_SYSTEM_NAME}.cmake module is mandatory.
The module will be loaded after CMakeGenericSystem.cmake so it gets
the final say in values of variables like CMAKE_FIND_LIBRARY_SUFFIXES.
This is why it works for Catamount.  You need to create a module like
Catamount.cmake for your own target platform, and put it somewhere
referenced by CMAKE_MODULE_PATH (Alex, can you confirm this?).

-Brad



More information about the CMake mailing list