强制CMake使用静态库 [英] Force CMake to use static libraries

查看:779
本文介绍了强制CMake使用静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[从CMake帮助列表无耻地交叉发布]

[Shamelessly cross-posted from the CMake help list]

我试图尽可能静态地创建二进制文件。我已经得到的fortran代码有X11和quadmath作为依赖,我遇到了一些问题(也许每个问题应该在一个不同的问题?):

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 question?):


  • 我的变数目前是

  • 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(SHARED_LIBS OFF)
set(STATIC_LIBS ON)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

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


使用这些,CMake尝试静态(如预期)建立每个程序 - 但是,它失败,因为我没有 Xaw.a - 我不知道这是否应该存在。我安装了最新的 libxaw7-dev ,我期望解决它。一个选择是自己编译X11库,但我真的不想这样做。

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我只注释了 set(CMAKE_EXE_LINKER_FLAGS$ {CMAKE_EXE_LINKER_FLAGS} -static),然后CMake编译所有内容, 即使我在我的 find_library()调用中指定 .a X11库的位置。我期待CMake使用.a文件,它可以,然后只使用共享库 - 是否有一种方法来强制这种行为?

  • 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?

但对此处描述的错误进行修复: http://gcc.gnu.org /bugzilla/show_bug.cgi?id=46539 ;因此gfortran似乎不能静态链接libquadmath?我尝试使用gcc修复,但我不能让CMake识别libgfortran标志:

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.

,正如你可能已经注意到的,我设置了libquadmath.a;当我使用

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")

然后程序编译成功(运行ldd报告不是动态可执行文件)

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?

推荐答案

我的意思是这个bug已经修复,猜猜你的问题不是那么相关,我不知道所有的答案。

I guess your questions are not that much related, I don't know the answer for all of them.

对于你的静态链接问题,因为你使用GCC,你可以传递多重静态和动态标志给它:

For your static linking problems, since you're using GCC, you can pass multiple -static and -dynamic flags to it:

set(CMAKE_EXE_LINKER_FLAGS "-static ${STATIC_LIBS} -dynamic ${EVERYTHING ELSE} -static ${MORE_STATIC_LIBS}")

我不知道为什么 Xaw.a 在您的系统上不可用,可能是因为您的Linux发行版的软件包维护者没有真正使它们可用。

I don't know why Xaw.a isn't available on your system, probably because the package maintainer of your Linux distribution didn't really make them available.

此外,编译一切静态可能会使所有发行版之间不兼容,并且削弱了其他人使用您的程序使用改进的最新库的能力,它可能不是你想要的。

Also, compiling everything static might make things not compatible between all distros out there and you cripple the ability for others to use improved, up-to-date libraries with your program, it might not be what you want.

如果你打算做一个自包含的程序包,最好只是包括你使用的共享库在一起,像Dropbox和许多其他专有应用程序(Humble Bundle游戏是其他示例)。

If you intend to make a self-contained package of your program, it might be better just to include the shared libraries you used together, like Dropbox and many other proprietary applications do (Humble Bundle games are other example).

这篇关于强制CMake使用静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆