内联在源文件中

发布于 2024-12-11 20:09:11 字数 295 浏览 0 评论 0原文

我有一个包含很多实用函数的大类。这些函数非常小,我希望它们内联。

问题是它们都在源文件中,应该保留在源文件中,而不是移动到头文件中(所以我不需要每次更改时都重新编译所有内容)。

如果我将它们标记为内联,我会得到

找不到符号

有没有办法使它们内联,或者我是否需要盲目信任链接时间优化器?
我需要代码能够在 clang 3 和 gcc 4.6 之间移植,但是基于编译器的 #define 就可以了(所以回答如何仅在其中一个编译器中执行此操作也可以)。

I have a big class with lots of utility functions. those functions are very small and I would like them inlined.

The problem is they are all in a source file and should stay in the source file and not move to the header file (so I don't need to recompile everything every time one changes).

If I mark them as inline I get

symbols not found

Is there a way to make them inline or do I need to blindly trust the link time optimizer?
I need the code to be portable between clang 3 and gcc 4.6, but #defines based on compiler will be ok (so answer how to do it only in one of the compilers is fine too).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二智少女猫性小仙女 2024-12-18 20:09:12

将实现放入 .inl 文件中。只有必要的.cpp 文件#include 就可以了。通过这种方式,对实现的更改(触摸 .inl 文件)只会触发依赖的 .cpp 文件的重新编译。对定义的更改(触摸 .h 文件)会触发使用该声明的所有文件的重新编译。

这是内联函数和模板实现的常见做法。 .inl 文件基本上应被视为“包含的 cpp”文件。

Place the implementation in an .inl file. Have only the necessary .cpp files #include it. This way changes to the implementation (touch .inl file) trigger recompile only of the dependent .cpp file. Changes to the definition (touch the .h file) trigger recompile of all files consuming the declaration.

This is a common practice for both inline functions and for template implementations. .inl files should be viewed basically as 'included cpp' files.

姐不稀罕 2024-12-18 20:09:12

[这些]函数非常小,我希望它们内联。 [但是]我不想[想]每次更改时都重新编译所有内容。

你不能同时拥有这两者。如果函数是内联的,那么您别无选择,只能在函数发生更改时重新编译所有调用方。这就是内联的工作原理。即使您使用链接时优化器在链接时自动执行此操作,您仍然需要支付重新处理所有调用方的编译时成本。

顺便说一句,据我所知,gcc 4.6 和 clang 3 都没有达到标准的链接时优化器。

编辑旁白:据我所知,还没有编译器具有足以使手动内联注释变得不必要的启发式方法。即使是我在评论中提到的 VS2010,作为链接时优化器的示例, 已经达到了标准,仍然需要很多关于内联内容的建议。

[These] functions are very small and I would like them inlined. [But] I don't [want] to recompile everything every time one changes.

You can't have both of these things. If a function is inlined, then you have no choice but to recompile all its callers when it changes. That's how inlining works. Even if you used a link-time optimizer to do it automatically at link time, you would still be paying the compilation-time cost of reprocessing all the callers.

AFAIK neither gcc 4.6 nor clang 3 have link-time optimizers that are up to scratch, by the way.

Editorial aside: No compiler that I know of has heuristics that are good enough to make manual inline annotations unnecessary, yet. Even VS2010, which I mentioned in the comments as an example of a link-time optimizer that is up to scratch, still needs quite a bit of advice about what to inline.

压抑⊿情绪 2024-12-18 20:09:12

如果您希望函数内联,则必须将其放置在头文件中,除非它仅在同一个源文件中使用。原因是编译器需要实际的函数定义,以便将定义“内联”放置在调用的任何地方,然后编译它。

您可以在此处此处

If you want a function to be inline'd you have to place it in a header file, unless it's only being used in the same source file. The reason being that the compiler needs the actual function definition in order to place the definition "inline" wherever it's being called, and then compile it up.

You can find further information: here and here.

淡淡離愁欲言轉身 2024-12-18 20:09:12

您需要强制内联该函数 - 将其放置在类定义中。

You need the function mandatory inlined - you place it in the class definition.

夏有森光若流苏 2024-12-18 20:09:12

一种选择是将内联函数放在预编译头文件中,这将加快编译速度。但由于内联函数的性质,所有使用它们的地方都必须重新编译。

http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

One option could be to place the inline functions in a precompiled header file, which will speed up the compilation. But because of the nature of inlining functions all places they are used will have to be recompiled.

http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文