[CMake] Using static libraries

Chris Wood c.c.wood at gmail.com
Wed Sep 18 12:24:22 EDT 2013


I'm trying to create binaries as statically as possible.  The fortran code
I've got has got X11 and quadmath as dependencies, and I've come across a
number of issues (maybe each of these issues should be in a different
email?):

 - My variables are currently

set(CMAKE_LIBRARY_PATH /usr/X11/lib /usr/X11/include/X11
${CMAKE_LIBRARY_PATH})
find_package(X11 REQUIRED)
find_library(X11 NAMES X11.a PATHS /usr/X11/include/X11/ /usr/X11/lib)
find_library(X11_Xaw_LIB NAMES Xaw Xaw /usr/X11/include/X11/ /usr/X11/lib
${X11_LIB_SEARCH_PATH})
find_library(Xaw Xaw7 PATHS ${X11_LIB_SEARCH_PATH})

set(CMAKE_LIBRARY_PATH /usr/lib/gcc/x86_64-linux-gnu/4.7
/usr/lib/gcc/x86_64-linux-gnu/4.7/x32 /usr/lib/gcc/x86_64-linux-gnu/4.7/32
${CMAKE_LIBRARY_PATH})

find_library(quadmath NAMES quadmath.a)

set(BUILD_SHARED_LIBS ON)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(LINK_SEARCH_START_STATIC TRUE)
set(LINK_SEARCH_END_STATIC TRUE)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")

Using these, CMake attempts to build *every* program statically (as
expected) - however, it fails because I don't have Xaw.a - I can't find out
whether this actually should exist.  I have installed the latest
libxaw7-dev which I was expecting to fix it. One option would be to compile
the X11 libraries myself, but I don't really want to do that...

 - if I comment out only 'set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -static")', then CMake compiles everything,
*but* uses shared libraries for every program, even though I specify the
location of .a X11 libraries in my find_library() calls. I was expecting
CMake to use the .a files where it could and then only use shared libraries
- is there a way to force this behaviour?

 - does anyone know yet of a fix for the bug described here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46539; whereby gfortran
seemingly can't statically link libquadmath? I tried the fix using gcc but
I can't get CMake to recognise the libgfortran flag:

  cmake -DCMAKE_Fortran_COMPILER=gcc -DCMAKE_Fortran_FLAGS=-gfortran

results in

-- The Fortran compiler identification is unknown
-- Check for working Fortran compiler: /usr/bin/gcc
-- Check for working Fortran compiler: /usr/bin/gcc  -- broken
CMake Error at
/usr/share/cmake-2.8/Modules/CMakeTestFortranCompiler.cmake:54 (message):
  The Fortran compiler "/usr/bin/gcc" is not able to compile a simple test
  program.

*However*, as you might have noticed, I set the location of the
libquadmath.a; when I build a program which doesn't use X11 but does use
quadmath when I use

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")

then the program does compile successfully (running ldd reports 'not a
dynamic executable') - does this mean that the bug has been fixed, or does
it only work because I set the location in CMake?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130918/27fb1d68/attachment.htm>


More information about the CMake mailing list