我想用std::regex
(標準正則表達式庫)編譯一個小型的C++程序。
編譯器:GCC /克++ 4.9.2在Fedora 21.巨大的程序大小C++與std :: regex
#include <string.h>
#include <iostream>
#include <regex>
using namespace std;
int main() {
cout << "Content-Type: text/html\n\n";
std::string s ("there is a subsequence in the string\n");
std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"
std::cout << std::regex_replace (s,e,"sub-$2");
}
這是不可能用std::regex
編譯程序而不-std=c++11
,所以對於在終端編譯合適的指令是:
g++ -std=c++11 code.cpp -o prog
我主要問題是:源代碼非常小,但爲什麼編譯後程序的最終文件大小如此巨大:480千字節?
是否因爲-std=c++11
的影響?
發生了什麼以及如何縮小最終二進制程序的大小?
UPD1。
使用-Os標誌實際上是將std :: regex的程序大小從480 KB降低到100-120 KB的好方法。 但奇怪的是,對於C/C++程序,使用std :: regexp的優化文件甚至超過了標準的7-12 kylobytes,而且只有很少的字符串源代碼。 例如,有可能把同樣的正則表達式與RE2正則表達式庫替換招(Fedora的21 「百勝安裝RE2-devel的」)在8.5 KB二進制文件:
#include <string.h>
#include <iostream>
#include "re2/re2.h"
using namespace std;
int main() {
cout << "Content-Type: text/html\n\n";
std::string s ("there is a subsequence in the string\n");
RE2::GlobalReplace(&s, "\\b(sub)([^ ]*)", "sub-\\2");
cout << s;
}
編譯時:
g++ -lre2 code.cpp -o prog
UPD2。
objdump的用於標準::正則表達式的程序:
0 .interp 00000013 08048154 08048154 00000154 2**0
1 .note.ABI-tag 00000020 08048168 08048168 00000168 2**2
2 .note.gnu.build-id 00000024 08048188 08048188 00000188 2**2
3 .gnu.hash 000000b0 080481ac 080481ac 000001ac 2**2
4 .dynsym 000006c0 0804825c 0804825c 0000025c 2**2
5 .dynstr 00000b36 0804891c 0804891c 0000091c 2**0
6 .gnu.version 000000d8 08049452 08049452 00001452 2**1
7 .gnu.version_r 000000d0 0804952c 0804952c 0000152c 2**2
8 .rel.dyn 00000038 080495fc 080495fc 000015fc 2**2
9 .rel.plt 000002b8 08049634 08049634 00001634 2**2
10 .init 00000023 080498ec 080498ec 000018ec 2**2
11 .plt 00000580 08049910 08049910 00001910 2**4
12 .text 0001f862 08049e90 08049e90 00001e90 2**4
13 .fini 00000014 080696f4 080696f4 000216f4 2**2
14 .rodata 00000dc8 08069740 08069740 00021740 2**6
15 .eh_frame_hdr 00003ab4 0806a508 0806a508 00022508 2**2
16 .eh_frame 0000f914 0806dfbc 0806dfbc 00025fbc 2**2
17 .gcc_except_table 00000e00 0807d8d0 0807d8d0 000358d0 2**2
18 .init_array 00000008 0807feec 0807feec 00036eec 2**2
19 .fini_array 00000004 0807fef4 0807fef4 00036ef4 2**2
20 .jcr 00000004 0807fef8 0807fef8 00036ef8 2**2
21 .dynamic 00000100 0807fefc 0807fefc 00036efc 2**2
22 .got 00000004 0807fffc 0807fffc 00036ffc 2**2
23 .got.plt 00000168 08080000 08080000 00037000 2**2
24 .data 00000240 08080180 08080180 00037180 2**6
25 .bss 000001b4 080803c0 080803c0 000373c0 2**6
26 .comment 0000002c 00000000 00000000 000373c0 2**0
objdump的用於RE2程序:
0 .interp 00000013 08048154 08048154 00000154 2**0
1 .note.ABI-tag 00000020 08048168 08048168 00000168 2**2
2 .note.gnu.build-id 00000024 08048188 08048188 00000188 2**2
3 .gnu.hash 00000034 080481ac 080481ac 000001ac 2**2
4 .dynsym 00000180 080481e0 080481e0 000001e0 2**2
5 .dynstr 00000298 08048360 08048360 00000360 2**0
6 .gnu.version 00000030 080485f8 080485f8 000005f8 2**1
7 .gnu.version_r 000000a0 08048628 08048628 00000628 2**2
8 .rel.dyn 00000010 080486c8 080486c8 000006c8 2**2
9 .rel.plt 00000090 080486d8 080486d8 000006d8 2**2
10 .init 00000023 08048768 08048768 00000768 2**2
11 .plt 00000130 08048790 08048790 00000790 2**4
12 .text 00000332 080488c0 080488c0 000008c0 2**4
13 .fini 00000014 08048bf4 08048bf4 00000bf4 2**2
14 .rodata 00000068 08048c08 08048c08 00000c08 2**2
15 .eh_frame_hdr 00000044 08048c70 08048c70 00000c70 2**2
16 .eh_frame 0000015c 08048cb4 08048cb4 00000cb4 2**2
17 .gcc_except_table 00000028 08048e10 08048e10 00000e10 2**0
18 .init_array 00000008 08049ee4 08049ee4 00000ee4 2**2
19 .fini_array 00000004 08049eec 08049eec 00000eec 2**2
20 .jcr 00000004 08049ef0 08049ef0 00000ef0 2**2
21 .dynamic 00000108 08049ef4 08049ef4 00000ef4 2**2
22 .got 00000004 08049ffc 08049ffc 00000ffc 2**2
23 .got.plt 00000054 0804a000 0804a000 00001000 2**2
24 .data 00000004 0804a054 0804a054 00001054 2**0
25 .bss 00000090 0804a080 0804a080 00001058 2**6
26 .comment 0000002c 00000000 00000000 00001058 2**0
主要區別是在12.text:在第一種情況下使用尺寸 - 0001f862(129122);第二 - 只有00000332(818)。
爲什麼你認爲這是巨大的?你期望什麼?爲什麼? – Walter 2015-03-08 20:15:16
運行'strip。/ prog'並檢查文件大小是否改變。 – 2015-03-08 20:26:25
@Walter,我試圖在UPD1中說明問題。我期望帶有三行代碼的程序將以小二進制編譯,但std :: regex與-std = C++ 11會返回不成比例的巨大二進制結果。所以我知道:std :: regex是正常的,還是我對C++ 11或std :: regex缺乏瞭解。 – 2015-03-08 20:47:20