C++版本广泛的用到了template。

Code bloat occurs because compilers generate code for all templated functions in each translation unit that use them. Back in the day the duplicate code was not consolidated resulting in “code bloat”. These days the duplicate code can be removed at link time.

所以,在看到产生的代码的大小后不要过于惊慌。(另外,编译速度也会比较慢。)

在3.4.0后的版本里,behaviac已经支持了Link Time Optimization(LTO)。LTO可以极大的减少产生代码的大小以及优化产生代码的效率。

gcc

  • 如下所示,通过参数指定Release以及ForeUseRelease可以打开LTO(如果你的gcc支持的话)
    cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DBEHAVIAC_VERSION_MODE=ForeUseRelease --build ../../../..
  • 或者通过cmake gui设置CMAKE_BUILD_TYPEReleaseBEHAVIAC_VERSION_MODEForeUseRelease
  • gcc版本需要是4.9以上,低版本不支持LTO
  • 其他版本的gcc请参考相应文档设置LTO

msvc

  • 在visual studio中可以参考打开编译选项/Gy, /GL及/OPT:ICF /OPT:REF /LTCG链接选项
  • 指定ForeUseRelease的时候,cmake生成的项目文件,在Release下缺省的已经打开上述优化开关。
  • 也可以考虑调整O1,O2或Ox编译选项

请参考构建说明

发表评论

电子邮件地址不会被公开。 必填项已用*标注