提升静态链接 [英] Boost static linking

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

问题描述

我使用在Linux中,GCC Boost库。安装和建设上去以后,我发现,使用正则表达式和线程使用共享Boost库计划。对于我而言,我需要静态链接。
如何更改连接类型?我应该重建升压,也许我可以通过我自己的项目定义一些常数设置连接类型或升压配置文件?

I am using the Boost library in Linux, GCC. After installing and building the Boost, I found that programs using Regex and Thread use shared Boost libraries. For my purposes, I need static linking. How can I change linking type? Should I rebuild the Boost, or maybe I can set linking type by defining some constant in my own projects or Boost configuration files?

推荐答案

只需添加 -static 来构建调用。下面是一个简单的例子会话:

Just add -static to your build invocation. Here is a quick example session:

$ cat boost_formatted_time.cpp
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

using namespace boost::posix_time;
using namespace std;

int main(int argc, char **argv) {
  time_facet *facet = new time_facet("%d-%b-%Y %H:%M:%S");
  cout.imbue(locale(cout.getloc(), facet));
  cout << second_clock::local_time() << endl;
}
$ g++ -o /tmp/bft_dyn boost_formatted_time.cpp -lboost_date_time
$ g++ -static -o /tmp/bft_stc boost_formatted_time.cpp -lboost_date_time
$ ls -lh /tmp/bft_*
-rwxr-xr-x 1 edd edd 216K 2010-02-24 12:34 /tmp/bft_dyn    
-rwxr-xr-x 1 edd edd 1.5M 2010-02-24 12:34 /tmp/bft_stc    
$ /tmp/bft_dyn
24-Feb-2010 12:34:55
$ /tmp/bft_stc
24-Feb-2010 12:34:59
$

请注意静态二进制如何1.5MB,而不是216KB的动态链接的变体。
所有与默认升压包的Debian测试完成。

Note how the static binary is 1.5mb as opposed to 216kb for the dynamically-linked variant. All done on Debian testing with the default Boost packages.

这篇关于提升静态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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