在 G++ 中使用 -frepo 选项

lon*_*ppl 4 c++ templates g++

我的工作地点是为一些没有虚拟内存的绝对古老的硬件开发代码。正因为如此,我们会尽量减少对象和二进制文件的文本大小,以免内存不足。这可能意味着我们需要限制使用来自 STL 的模板,或者完全禁止它们。在环顾四周寻找最小化文件大小并仍然使用模板的方法时,我遇到了-frepog++的选项。几次测试后,我比开始时更加困惑。使用 -frepo 时,最终的二进制文件大小相同或更大,这对我来说没有意义。任何人都可以向我解释这个选项实际上做了什么(除了“它只是有效”,因为这是 GCC 4.7.1 手册中的解释)以及我可能如何滥用它?

编译g++ -c -frepo main.cpp test8.cpp和链接 正在g++ test8.o main.o 创建 .rpo 文件。

测试8.h:

#include <list>
using namespace std;

class Test
{
  public:
    Test ();
    list<int> makeNewIntList ();
  private:
  list<int> intList;
};
Run Code Online (Sandbox Code Playgroud)

测试8.cpp:

#include "test8.h"
#include <list>

using namespace std;

Test::Test()
{
  intList = list<int>( 10, 12 );
}

list<int> Test::makeNewIntList()
{
  intList.push_back(4);
  return intList;
}
Run Code Online (Sandbox Code Playgroud)

主程序

#include "test8.h"

using namespace std;

void findFive (int num);
list<int> makeIntList();

int main( int argc, char* argv[])
{
  Test test;
  list<int> intList = test.makeNewIntList();
  list<int> intList2 = makeIntList();
  list<float> floatList = list<float> (10,12);
  floatList.push_back(5);
}

list<int> makeIntList()
{
  list<int> intList = list<int> (10,12);
  return intList;
}
Run Code Online (Sandbox Code Playgroud)

我们正在使用 GCC 4.1.2。还要注意 GCC 4.7.0 也好不到哪里去,升级编译器也不是一个可行的解决方案。

Jon*_*ely 5

我建议你忘记-frepo,它是一个遗物,几乎没有使用过。

相反,您可以查看extern template用于声明显式实例化的语法,以防止模板被隐式实例化,从而允许您控制实例化以便它们只发生在一个地方。确保您不会错过任何可以编译的内容-fno-implicit-templates(如果您严格要求在extern template任何必要的地方使用声明,则没有必要。)