[CMake] Static linking and find_library

Alexander Neundorf a.neundorf-work at gmx.net
Wed May 13 16:17:13 EDT 2009


On Wednesday 13 May 2009, Brad King wrote:
> 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?)

Well, after all, it's just a cmake variable, so it can also be set from the 
command line.
If it is already set when cmake would try to determine the system, it 
interprets this as cross compiling, i.e. CMAKE_SYSTEM is already set because 
the system you want to build for is not the current host system, but a 
different one, and that's why (and only that's why) it should be set.

> > 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.

This disables the building of shared libraries and produces warnings 
for add_library(SHARED).

> > 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.

Yes.
It should only preset the things cmake can not automatically detect when cross 
compiling, i.e. the target system name, where the target environment is 
located, and which compiler to use.

> 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?).

Yes.

Alex


More information about the CMake mailing list